import type { TranslateFn } from '../i18n/useTranslation';

export type UploadErrorDialog = {
  code: string;
  title: string;
  description: string;
  hint?: string;
  tone: 'danger' | 'warning' | 'info';
};

function formatWithNumbers(template: string, values: Record<string, number | string | undefined>): string {
  return Object.entries(values).reduce((acc, [key, value]) => {
    if (value === undefined) {
      return acc;
    }

    return acc.replaceAll(`{${key}}`, String(value));
  }, template);
}

export function resolveUploadErrorDialog(
  code: string | undefined,
  meta: Record<string, unknown> | undefined,
  t: TranslateFn
): UploadErrorDialog {
  const normalized = (code ?? 'unknown').toLowerCase();
  const getNumber = (value: unknown): number | undefined => (typeof value === 'number' ? value : undefined);

  switch (normalized) {
    case 'photo_limit_exceeded': {
      const used = getNumber(meta?.used);
      const limit = getNumber(meta?.limit);
      const remaining = getNumber(meta?.remaining);

      return {
        code: normalized,
        tone: 'danger',
        title: t('upload.dialogs.photoLimit.title'),
        description: formatWithNumbers(t('upload.dialogs.photoLimit.description'), {
          used,
          limit,
          remaining,
        }),
        hint: t('upload.dialogs.photoLimit.hint'),
      };
    }

    case 'upload_device_limit':
      return {
        code: normalized,
        tone: 'warning',
        title: t('upload.dialogs.deviceLimit.title'),
        description: t('upload.dialogs.deviceLimit.description'),
        hint: t('upload.dialogs.deviceLimit.hint'),
      };

    case 'event_package_missing':
    case 'event_not_found':
      return {
        code: normalized,
        tone: 'info',
        title: t('upload.dialogs.packageMissing.title'),
        description: t('upload.dialogs.packageMissing.description'),
        hint: t('upload.dialogs.packageMissing.hint'),
      };

    case 'gallery_expired':
      return {
        code: normalized,
        tone: 'danger',
        title: t('upload.dialogs.galleryExpired.title'),
        description: t('upload.dialogs.galleryExpired.description'),
        hint: t('upload.dialogs.galleryExpired.hint'),
      };

    case 'csrf_mismatch':
      return {
        code: normalized,
        tone: 'warning',
        title: t('upload.dialogs.csrf.title'),
        description: t('upload.dialogs.csrf.description'),
        hint: t('upload.dialogs.csrf.hint'),
      };

    default:
      return {
        code: normalized,
        tone: 'info',
        title: t('upload.dialogs.generic.title'),
        description: t('upload.dialogs.generic.description'),
        hint: t('upload.dialogs.generic.hint'),
      };
  }
}
