import { createI18n } from 'vue-i18n';
import {Locale} from 'vant'
import zhCN from 'vant/lib/locale/lang/zh-CN'
import idId from 'vant/lib/locale/lang/id-ID'
import hiIN from 'vant/lib/locale/lang/hi-IN'
import enUS from 'vant/lib/locale/lang/en-US'
import zhLocale from './zh_cn'
import idLocale from './id_ID'
import hiLocale from './hi-IN'
import enLocale from './en_us'
import type { App } from 'vue';


const messages = {
  zh: {
    ...zhCN,
    ...zhLocale
  },
  id: {
    ...idId,
    ...idLocale
  },
  hi: {
    ...hiIN,
    ...hiLocale
  },
  en: {
    ...enUS,
    ...enLocale
  }
}

// const language = (navigator.language || 'zh').toLocaleLowerCase(); // 这是获取浏览器的语言
const defLanguage = localStorage.getItem('lang') || 'zh_CN';

// console.log('当前的环境语言是：', language)
console.log('当前的后台语言是：', localStorage.getItem('lang'))
const localeLanMap = {
  'zh_CN': 'zh',
  'hi_IN': 'hi',
  'id_ID': 'id',
  'en_US': 'en',
}
const i18n = createI18n({
  allowComposition: true,
  legacy: false,
  locale: localeLanMap[defLanguage],  // 设置默认语言
  fallbackLocale: localeLanMap[defLanguage], // 设置备用语言
  messages: messages // 设置资源文件对象
})

// 更新vant组件库本身的语言变化，支持国际化
function vantLocales (app: App<Element>) {
  app.use(i18n)
  const lanMap = {
    'zh_CN': zhCN,
    'hi_IN': hiIN,
    'id_ID': idId,
    'en_US': enUS,
  }

  Locale.use(defLanguage, lanMap[defLanguage] || zhCN)

  // if (lan === 'hi') {
  //   Locale.use(lan, hiIN)
  //   console.log('========================', lan)
  // } else if (lan === 'id') {
  //   Locale.use(lan, idId)
  // } else {
  //   Locale.use(lan, zhCN)
  // }
}

export {i18n, vantLocales}
