import Vue from 'vue'
import VueI18n from 'vue-i18n'
import { getStorage, setStorage } from '@/utils/localStorage'
import { Locale } from 'vant';
import zhCn from './zh-CN'
import zhTw from './zh-TW'
import en from './en'
import ru from './ru'
import th from './th'
import vi from './vi'
import vZhCn from "./vant/zh-CN";
import vZhTw from "./vant/zh-TW";
import vEn from "./vant/en";
import vRu from "./vant/ru";
import vTh from "./vant/th";
import vVi from "./vant/vi";
const vantLang = {
  'zh-TW': vZhTw,
  'zh-CN': vZhCn,
  'en': vEn,
  'ru': vRu,
  'th': vTh,
  'vi': vVi,
}
Vue.use(VueI18n)
const hasLanguageList = ['zh-CN', 'zh-TW', 'en', 'ru', 'th', 'vi'];
const hasLanguageNaList = [/\/zh-CN\/|\/zh-CN$/, /\/zh-TW\/|\/zh-TW$/, /\/en\/|\/en$/, /\/ru\/|\/ru$/, /\/th\/|\/th$/, /\/vi\/|\/vi$/];
const messages = {
  'zh-CN': {
    ...zhCn,
  },
  'zh-TW': {
    ...zhTw,
  },
  'en': {
    ...en,
  },
  'ru': {
    ...ru,
  },
  'th': {
    ...th,
  },
  'vi': {
    ...vi,
  },
}
export function getLanguage() {
  var language = '';
  hasLanguageNaList.forEach((item, index) => {
    if (item.test(window.location.pathname)) {
      language = hasLanguageList[index]
    }
  })
  if (language) {
    setStorage('language', language)
    return language
  }
  language = getStorage('language')
  if (language) {
    return language
  }
  var lang = {
    'zh-CN': 'zh-CN',
    'zh': 'zh-TW',
    'zh-TW': 'zh-TW',
    'en': 'en',
    'en-US': 'en',
  };
  var language1 = lang[navigator.language];
  if (hasLanguageList.indexOf(language1) !== -1) {
    language = language1
  }
  setStorage('language', language || 'en')
  return language || 'en'
}

const i18n = new VueI18n({
  locale: getLanguage(),
  messages
})

export function setVantLang(lan) {
  Locale.use(lan, vantLang[lan]);
}
setVantLang(i18n.locale)
export default i18n
