import i18n from 'i18next';
import {initReactI18next, useTranslation} from 'react-i18next';
import fr from '@/lib/locales/fr.json';
import en from '@/lib/locales/en.json';

i18n.use(initReactI18next).init({
    resources: {
        fr: {translation: fr},
        en: {translation: en},
    },
    lng: 'fr',
    fallbackLng: 'fr',
    interpolation: {escapeValue: false},
});

export function useTranslations(namespace?: string) {
    const {t} = useTranslation();
    if (namespace) {
        return (key: string, params?: Record<string, unknown>) =>
            t(`${namespace}.${key}`, params as Record<string, string>);
    }
    return (key: string, params?: Record<string, unknown>) =>
        t(key, params as Record<string, string>);
}

export function changeLanguage(lang: string) {
    i18n.changeLanguage(lang);
}

export {i18n};
