import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import * as RNLocalize from 'react-native-localize';
import uz from './locales/uz.json';
import ru from './locales/ru.json';

type TranslationResources = {
    [key: string]: {
        translation: Record<string, string>;
    };
};

const resources: TranslationResources = {
    uz: { translation: uz },
    ru: { translation: ru },
};

const languageDetector = {
    type: 'languageDetector',
    async: true,
    detect: (callback: (lang: string) => void) => {
        const bestLang = RNLocalize.findBestLanguageTag(['uz', 'ru']);
        callback(bestLang?.languageTag ?? 'uz');
    },
    init: () => { },
    cacheUserLanguage: () => { },
};

i18n
    .use(languageDetector as any)
    .use(initReactI18next)
    .init({
        fallbackLng: 'uz',
        resources,
        interpolation: {
            escapeValue: false,
        },
    });

export default i18n;
