import i18n from 'i18next'
import { initReactI18next } from 'react-i18next'

// Dynamically import all json files in the locales directory
const modules = import.meta.glob('./locales/*.json', { eager: true })

const resources = {}
const supportedLngs: string[] = []

for (const path in modules) {
    const key = path.split('/').pop()?.replace('.json', '')
    if (key) {
        resources[key] = { translation: (modules[path] as any).default || modules[path] }
        supportedLngs.push(key)
    }
}

i18n
    .use(initReactI18next)
    .init({
        resources,
        lng: 'tr',
        fallbackLng: 'en',
        supportedLngs,
        interpolation: {
            escapeValue: false
        }
    })

export { supportedLngs }
export default i18n
