import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import Backend from 'i18next-http-backend';
import LanguageDetector from 'i18next-browser-languagedetector';

const supportedLngs = ['de', 'en'];
const fallbackLng = 'de';

const detection = {
  order: ['path', 'localStorage', 'cookie', 'htmlTag', 'navigator'],
  lookupFromPathIndex: 0,
  caches: ['localStorage'],
};

i18n
  .use(Backend)
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    fallbackLng,
    supportedLngs,
    ns: ['marketing', 'auth', 'common', 'legal'],
    defaultNS: 'marketing',
    debug: import.meta.env.DEV,
    load: 'languageOnly',
    detection,
    interpolation: {
      escapeValue: false,
    },
    backend: {
      // Cache-bust to ensure fresh translations when files change.
      loadPath: '/lang/{{lng}}/{{ns}}.json?v=20250212',
    },
    react: {
      useSuspense: true,
    },
  });

export default i18n;
