import { createI18n, useI18n } from 'vue-i18n'
import { config } from '../settings/application'
import dayjs from 'dayjs'
import { VxeUI } from 'vxe-table'
import zh from './zh'
import en from './en'

import en_US_ant from 'ant-design-vue/es/locale/en_US'
import zh_CN_ant from 'ant-design-vue/es/locale/zh_CN'

import zhCN_vxe from 'vxe-table/lib/locale/lang/zh-CN'
import enUS_vxe from 'vxe-table/lib/locale/lang/en-US'

VxeUI.setI18n('zh-CN', zhCN_vxe)
VxeUI.setI18n('en-US', enUS_vxe)

import day_zh from 'dayjs/locale/zh-cn'
import day_en from 'dayjs/locale/en'
export const messages = {
	zh_CN: {
		...zh,
		...zh_CN_ant,
		...zhCN_vxe,
	},
	en_US: {
		...en,
		...en_US_ant,
		...enUS_vxe,
	},
}

const i18n = createI18n({
	legacy: false,
	globalInjection: true,
	locale: config.language,
	messages,
})

export function setI18nLanguage(lang: 'zh_CN' | 'en_US'): string {
	useI18n().locale.value = lang
	const html = document.querySelector('html')
	html?.setAttribute('lang', lang)
	dayjs.locale(lang === 'zh_CN' ? day_zh : day_en)
	VxeUI.setLanguage(lang === 'zh_CN' ? 'zh-CN' : 'en-US')
	return lang
}
/** 翻译 */
export function translate(key: string): string {
	return i18n.global.t(key)
}
export default i18n
