import { getLocales } from 'expo-localization';
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import ar from './translations/ar.json';
import de from './translations/de.json';
import en from './translations/en.json';
import es from './translations/es.json';
import fr from './translations/fr.json';
import it from './translations/it.json';
import ja from './translations/ja.json';
import pt from './translations/pt.json';
import ru from './translations/ru.json';
import zh from './translations/zh.json';

// Initialize i18n
const langList = { en, es, fr, de, it, pt, zh, ja, ru, ar };
const resources = {};
const locales = getLocales();

Object.keys(langList).forEach((lang) => {
    resources[lang] = { translation: langList[lang] };
});

i18n
    .use(initReactI18next)
    .init({
        resources,
        lng: locales[0]?.languageTag.split('-')[0], // Use the device's language
        fallbackLng: 'en',
        interpolation: {
            escapeValue: false, // React already escapes values
        },
    });

export default i18n;