import i18n from 'sveltekit-i18n';

/**
 * @readonly
 * @enum {string}
 */
export const SUPPORTED_LOCALES = Object.freeze({
  EN_US: 'en-US',
  PT_BR: 'pt-BR',
});

const getInitialLocale = () => {
  if (typeof localStorage !== 'undefined') {
    const saved = localStorage.getItem('locale');
    // @ts-ignore
    if (saved && Object.values(SUPPORTED_LOCALES).includes(saved)) {
      return saved;
    }
  }
  return SUPPORTED_LOCALES.PT_BR;
};

/** @type {import('sveltekit-i18n').Config<{ years?: number; sinceYear?: number; yearNoun?: string }>} */
const config = {
  initLocale: getInitialLocale(),
  fallbackLocale: SUPPORTED_LOCALES.EN_US,
  loaders: [
    {
      locale: SUPPORTED_LOCALES.PT_BR,
      key: 'common',
      loader: async () => (await import('./pt-BR/common.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.EN_US,
      key: 'common',
      loader: async () => (await import('./en-US/common.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.PT_BR,
      key: 'metaHome',
      routes: ['/'],
      loader: async () => (await import('./pt-BR/meta-home.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.EN_US,
      key: 'metaHome',
      routes: ['/'],
      loader: async () => (await import('./en-US/meta-home.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.PT_BR,
      key: 'header',
      loader: async () => (await import('./pt-BR/header.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.EN_US,
      key: 'header',
      loader: async () => (await import('./en-US/header.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.PT_BR,
      key: 'hero',
      routes: ['/'],
      loader: async () => (await import('./pt-BR/hero.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.EN_US,
      key: 'hero',
      routes: ['/'],
      loader: async () => (await import('./en-US/hero.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.PT_BR,
      key: 'brands',
      routes: ['/'],
      loader: async () => (await import('./pt-BR/brands.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.EN_US,
      key: 'brands',
      routes: ['/'],
      loader: async () => (await import('./en-US/brands.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.PT_BR,
      key: 'featured',
      routes: ['/'],
      loader: async () => (await import('./pt-BR/featured.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.EN_US,
      key: 'featured',
      routes: ['/'],
      loader: async () => (await import('./en-US/featured.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.PT_BR,
      key: 'about',
      routes: ['/'],
      loader: async () => (await import('./pt-BR/about.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.EN_US,
      key: 'about',
      routes: ['/'],
      loader: async () => (await import('./en-US/about.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.PT_BR,
      key: 'contact',
      routes: ['/'],
      loader: async () => (await import('./pt-BR/contact.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.EN_US,
      key: 'contact',
      routes: ['/'],
      loader: async () => (await import('./en-US/contact.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.PT_BR,
      key: 'footer',
      loader: async () => (await import('./pt-BR/footer.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.EN_US,
      key: 'footer',
      loader: async () => (await import('./en-US/footer.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.PT_BR,
      key: 'privacy',
      routes: ['/privacy-policy'],
      loader: async () => (await import('./pt-BR/privacy.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.EN_US,
      key: 'privacy',
      routes: ['/privacy-policy'],
      loader: async () => (await import('./en-US/privacy.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.PT_BR,
      key: 'embroidery',
      routes: ['/embroidery-viewer'],
      loader: async () => (await import('./pt-BR/embroidery.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.EN_US,
      key: 'embroidery',
      routes: ['/embroidery-viewer'],
      loader: async () => (await import('./en-US/embroidery.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.PT_BR,
      key: 'alagoas',
      routes: ['/entrepreneurship-alagoas'],
      loader: async () => (await import('./pt-BR/alagoas.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.EN_US,
      key: 'alagoas',
      routes: ['/entrepreneurship-alagoas'],
      loader: async () => (await import('./en-US/alagoas.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.PT_BR,
      key: 'implantCase',
      routes: ['/implant-file'],
      loader: async () => (await import('./pt-BR/implant-case.json')).default,
    },
    {
      locale: SUPPORTED_LOCALES.EN_US,
      key: 'implantCase',
      routes: ['/implant-file'],
      loader: async () => (await import('./en-US/implant-case.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);
      document.documentElement.lang = $locale;
    }
  }
});
