import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import * as Localization from 'expo-localization';
import { SPANISH_LANG } from './spanish_lang';
import { ENGLISH_LANG } from './english_lang';
import AsyncStorage from '@react-native-async-storage/async-storage';

i18n.use(initReactI18next).init({
  compatibilityJSON: 'v3', // Necesario para compatibilidad con Expo
  fallbackLng: 'en', // Idioma de respaldo
  resources: {
    es: {
      translation: SPANISH_LANG
    },
    en: {
      translation: ENGLISH_LANG
    }
  },
  interpolation: {
    escapeValue: false // React ya hace el escape de valores
  }
});

export enum Languages {
  ENGLISH = 'en',
  SPANISH = 'es'
};

export const AvailableLanguages = [
  {
    key: Languages.ENGLISH,
    value: 'English'
  },
  {
    key: Languages.SPANISH,
    value: 'Español'
  }
];

(async () => {
  const lang = await AsyncStorage.getItem("lang");
  if (lang) {
    i18n.changeLanguage(lang);
  }
})();

export default i18n;


