import { createI18n } from 'vue-i18n'
import { lang } from '@/composables/config'
import cn from '@/i18n/locales/cn'
import zh from '@/i18n/locales/zh'
import en from '@/i18n/locales/en'
import de from '@/i18n/locales/de'

const messages = {
  cn,
  zh,
  en,
  de
}

type LocaleType = 'cn' | 'en' | 'zh' | 'de'
// 确保在初始化时获取正确的语言设置
const savedLang = (localStorage.getItem('lang') as LocaleType) || 'cn'

const i18n = createI18n({
  legacy: false,
  locale: savedLang,
  fallbackLocale: 'en',
  messages,
  silentTranslationWarn: true,
  silentFallbackWarn: true,
  runtimeOnly: false,
  compositionOnly: true,
  // 添加全局注入
  globalInjection: true
})
// 监听语言变化
watch(lang, (newLang) => {
  i18n.global.locale.value = newLang as LocaleType
  localStorage.setItem('lang', newLang)
})

const localesList = ['cn', 'en'] as const
// const localesList = ['en','cn','zh', 'de','es','ar','id','ms','th','vi','ko','pt','fa','tr']

export { i18n, localesList }
