import i18n from 'i18next'
import { initReactI18next } from 'react-i18next'
import en from './en.json'
import ua from './ua.json'

export const LANG_STORAGE_KEY = 'azaion.lang'

function readPersistedLanguage(): 'en' | 'ua' {
  // Safari private mode throws on localStorage access — fall back to 'en'.
  try {
    const persisted = localStorage.getItem(LANG_STORAGE_KEY)
    return persisted === 'ua' || persisted === 'en' ? persisted : 'en'
  } catch {
    return 'en'
  }
}

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

export default i18n
