import { createI18n } from 'vue-i18n'
import zhCN from './zh-CN'
import en from './en'

// 界面语言:直接读 localStorage(避免 import @/stores/appSettings 形成循环依赖 TDZ)。
// appSettings.loadAll 也会从 SQLite 写入 localStorage,此处直读与 store 行为一致。
const getInitialLocale = (): string => {
  if (typeof window !== 'undefined') {
    try {
      return localStorage.getItem('df-language') || 'zh-CN'
    } catch {
      return 'zh-CN'
    }
  }
  return 'zh-CN'
}

const i18n = createI18n({
  legacy: false,
  globalInjection: true, // 模板中可直接用 $t / $tc
  locale: getInitialLocale(),
  fallbackLocale: 'en',
  messages: {
    'zh-CN': zhCN as Record<string, any>,
    en: en as Record<string, any>,
  },
})

export default i18n
