import { createI18n } from 'vue-i18n';
import zh from './zh.json';
import en from './en.json'
import es from './es.json'
import km from './km.json'
import verConfig from '../ver.config';
import { getStorage, setStorage } from '@/utils';

// setStorage(verConfig.langName, 'zh')
const lang = getStorage(verConfig.langName);
const defaultLang = verConfig.lang || "zh"
if (!lang) {
	setStorage(verConfig.langName, defaultLang);
}

export const messages = {
	zh: { ...zh },
	en,
	es,
	km
}
export const setLan = (_lan, callBack = () => { }) => {
	setStorage(verConfig.langName, _lan);
	i18n.global.locale = _lan;
	uni.setLocale(_lan)
	callBack()
};

export const getLan = () => {
	const lan = getStorage(verConfig.langName);
	if (!lan) {
		setLan(defaultLang);
		return defaultLang;
	}
	return lan;
};

export const lans = [
	{
		label: '中文简体',
		value: 'zh'
	},
	{
		label: 'English',
		value: 'en'
	},
	{
		label: 'Española',
		value: 'es'
	}
	,
	{
		label: 'Khmer',
		value: 'km'
	}
]

const locale = getLan();
export const i18n = createI18n({
	locale,
	messages,
})
export const findLang = (_lan) => {
	let find = lans.find(item => _lan === item.value)
	return find
}


export const t = (key, spareKey = {}) => {
	return i18n.global.t(key, spareKey)
}