import { ApiError } from '../api/client'; import { resolveLocaleTag } from '../i18n/language'; import i18n from '../i18n'; type TFn = (key: string, options?: Record) => string; /** * Etykieta błędu API dla użytkownika: kod → `errors:`, z obsługą * `details.until` (`errors:_until`) i `details.reason` (sufiks). * Bez mapowania — surowy message; bez message — fallback. */ export function apiErrorMessage(err: unknown, t: TFn, fallback?: string): string { if (err instanceof ApiError) { const code = err.body?.error?.code; if (code) { const details = (err.body?.error?.details ?? {}) as { until?: string; reason?: string | null; }; let base = ''; if (details.until) { const date = new Intl.DateTimeFormat(resolveLocaleTag(i18n.language), { dateStyle: 'long', timeStyle: 'short', }).format(new Date(details.until)); base = t(`errors:${code}_until`, { date, defaultValue: '' }); } if (!base) base = t(`errors:${code}`, { defaultValue: '' }); if (base) { if (details.reason) base += t('errors:reasonSuffix', { reason: details.reason }); return base; } } return err.message || fallback || ''; } if (err instanceof Error) return err.message || fallback || ''; return String(err); }