'use client'

import i18n from 'i18next'
import { initReactI18next } from 'react-i18next'
import LanguageDetector from 'i18next-browser-languagedetector'
import zhTW from './locales/zh-TW.json'
import zhCN from './locales/zh-CN.json'
import en from './locales/en.json'
import ja from './locales/ja.json'
import koKR from './locales/ko-KR.json'

const resources = {
    'zh-TW': { translation: zhTW },
    'zh-CN': { translation: zhCN },
    en: { translation: en },
    ja: { translation: ja },
    'ko-KR': { translation: koKR },
}

export const supportedLngs = [
    'zh-TW', 'zh-CN', 'en', 'ja', 'ko-KR',
] as const

if (!i18n.isInitialized) {
    i18n
        .use(LanguageDetector)
        .use(initReactI18next)
        .init({
            resources,
            fallbackLng: 'zh-TW',
            supportedLngs: [...supportedLngs],
            defaultNS: 'translation',
            interpolation: {
                escapeValue: false,
            },
            detection: {
                order: ['localStorage', 'navigator', 'htmlTag'],
                caches: ['localStorage'],
            },
            react: {
                useSuspense: false,
            },
        })
}

export default i18n
