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

function detectSystemLocale(): string {
  const saved = localStorage.getItem('locale')
  if (saved) return saved

  const lang = navigator.language
  if (lang.startsWith('zh')) return 'zh-CN'
  return 'en'
}

const defaultLocale = detectSystemLocale()

export type Locale = 'zh-CN' | 'en'

export const i18n = createI18n({
  legacy: false,
  locale: defaultLocale,
  fallbackLocale: 'en',
  messages: {
    'zh-CN': zhCN,
    en,
  },
})
