import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import * as RNLocalize from 'react-native-localize';

import en from './locales/en.json';
import es from './locales/es.json';

const resources = {
  en: { translation: en },
  es: { translation: es },
};

const fallbackLocale = 'en';
const deviceLocales = RNLocalize.getLocales();
const bestMatch = deviceLocales[0]?.languageCode ?? fallbackLocale;

void i18n
  .use(initReactI18next)
  .init({
    resources,
    lng: bestMatch,
    fallbackLng: fallbackLocale,
    interpolation: {
      // React already escapes values
      escapeValue: false,
    },
    compatibilityJSON: 'v4',
  });

export default i18n;
