import i18n from 'sveltekit-i18n';

/**
 * A frozen object mapping locale identifiers to their respective locale codes.
 *
 * These values represent the supported languages in the application.
 * Used for validating user preferences and loading the correct translations.
 *
 * @readonly
 * @enum {string}
 */
export const SUPPORTED_LOCALES = Object.freeze({
  EN_US: 'en-US',
  PT_BR: 'pt-BR',
});

/** @type {import('sveltekit-i18n').Config} */
const config = {
  initLocale: SUPPORTED_LOCALES.EN_US,
  fallbackLocale: SUPPORTED_LOCALES.EN_US,
  loaders: [
    {
      locale: SUPPORTED_LOCALES.EN_US,
      key: 'header',
      loader: async () => (await import('./en-US/header.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.EN_US,
      key: 'footer',
      loader: async () => (await import('./en-US/footer.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.EN_US,
      key: 'home',
      routes: ['/'],
      loader: async () => (await import('./en-US/home.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.EN_US,
      key: 'about',
      routes: ['/about'],
      loader: async () => (await import('./en-US/about.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.EN_US,
      key: 'donate',
      routes: ['/donate'],
      loader: async () => (await import('./en-US/donate.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.EN_US,
      key: 'privacy.policy',
      routes: ['/privacy-policy'],
      loader: async () => (await import('./en-US/privacy-policy.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.EN_US,
      key: 'mobile.app.privacy.policy',
      routes: ['/mobile-app/privacy-policy'],
      loader: async () =>
        (await import('./en-US/mobile-app-privacy-policy.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.EN_US,
      key: 'terms.of.service',
      routes: ['/terms-of-service'],
      loader: async () =>
        (await import('./en-US/terms-of-service.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.EN_US,
      key: 'viewer',
      routes: ['/viewer'],
      loader: async () => (await import('./en-US/viewer.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.PT_BR,
      key: 'header',
      loader: async () => (await import('./pt-BR/header.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.PT_BR,
      key: 'footer',
      loader: async () => (await import('./pt-BR/footer.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.PT_BR,
      key: 'home',
      routes: ['/'],
      loader: async () => (await import('./pt-BR/home.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.PT_BR,
      key: 'about',
      routes: ['/about'],
      loader: async () => (await import('./pt-BR/about.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.PT_BR,
      key: 'donate',
      routes: ['/donate'],
      loader: async () => (await import('./pt-BR/donate.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.PT_BR,
      key: 'privacy.policy',
      routes: ['/privacy-policy'],
      loader: async () => (await import('./pt-BR/privacy-policy.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.PT_BR,
      key: 'mobile.app.privacy.policy',
      routes: ['/mobile-app/privacy-policy'],
      loader: async () =>
        (await import('./pt-BR/mobile-app-privacy-policy.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.PT_BR,
      key: 'terms.of.service',
      routes: ['/terms-of-service'],
      loader: async () =>
        (await import('./pt-BR/terms-of-service.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.PT_BR,
      key: 'viewer',
      routes: ['/viewer'],
      loader: async () => (await import('./pt-BR/viewer.json')).default,
    },
  ],
};

export const {
  t,
  locale,
  locales,
  loading,
  loadTranslations,
  setRoute,
  setLocale,
} = new i18n(config);

locale.subscribe(($locale) => {
  if (typeof localStorage !== 'undefined' && $locale) {
    const existing = localStorage.getItem('locale');
    if (existing !== $locale) {
      localStorage.setItem('locale', $locale);
    }
  }
});
