import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import * as Localization from 'expo-localization';

import de from '../../../frontend/src/i18n/locales/de.json';
import enUS from '../../../frontend/src/i18n/locales/en-US.json';

const resources = {
  de: { translation: de },
  'en-US': { translation: enUS },
} as const;

function pickLanguageTag() {
  const tag = Localization.getLocales()?.[0]?.languageTag || 'de';
  if (tag in resources) return tag as keyof typeof resources;
  if (tag.startsWith('en')) return 'en-US';
  return 'de';
}

void i18n
  .use(initReactI18next)
  .init({
    resources,
    lng: pickLanguageTag(),
    fallbackLng: 'de',
    interpolation: { escapeValue: false },
  });

export default i18n;

