import useAppStore from '@/store/modules/app'

/**
 * 当前语言对应的后端语言
 */
export function serverLang() {
  const lang = useAppStore().lang;
  let language = 'en_US'
  if (lang == 'zh-cn') {
    language = 'zh_CN'
  } else if (lang == 'zh-tw') {
    language = 'zh_TW'
  } else if (lang == 'es') {
    language = 'es_ES'
  } else if (lang == 'ar') {
    language = 'ar_SA'
  } else if (lang == 'id') {
    language = 'id_ID'
  } else if (lang == 'ko') {
    language = 'ko_KR'
  } else if (lang == 'hi') {
    language = 'hi_IN'
  } else if (lang == 'th') {
    language = 'th_TH'
  } else if (lang == 'vi') {
    language = 'vi_VN'
  } else if (lang == 'en') {
    language = 'en_US'
  }
  return language
}

/**
 * 后端语言对应的前端语言
 * @param {String} lang 
 */
export function clientLang(lang) {
  let language = 'en_US'
  if (!lang) {
    return language;
  }
  if (lang == 'zh_CN') {
    language = 'zh-cn'
  } else if (lang == 'zh_TW') {
    language = 'zh-tw'
  } else if (lang == 'es_ES') {
    language = 'es'
  } else if (lang == 'hi_IN') {
    language = 'hi'
  } else if (lang == 'ko_KR') {
    language = 'ko'
  } else if (lang == 'id_ID') {
    language = 'id'
  } else if (lang == 'th_TH') {
    language = 'th'
  } else if (lang == 'vi_VN') {
    language = 'vi'
  } else if (lang == 'ar_SA') {
    language = 'ar'
  } else if (lang == 'en_US') {
    language = 'en'
  }
  return language;
}

/**
 * 从浏览器获取语言
 */
export function fromBrowserLang() {
  let currentLang = navigator.language;
  if(!currentLang) {
    currentLang = navigator.browserLanguage;
  }
  if (currentLang == 'zh-Hant' || currentLang == 'zh-MO' || currentLang == 'zh-TW' || currentLang == 'zh-HK') {
    currentLang = 'zh_TW'
  } else if (currentLang.startsWith('zh')) {
    currentLang = 'zh_CN'
  } else if (currentLang.startsWith('ar')) {
    currentLang = 'ar_SA'
  } else if (currentLang.startsWith('es')) {
    currentLang = 'es_ES'
  } else if (currentLang.startsWith('hi')) {
    currentLang = 'hi_IN'
  } else if (currentLang.startsWith('ko')) {
    currentLang = 'ko_KR'
  } else if (currentLang.startsWith('id')) {
    currentLang = 'id_ID'
  } else if (currentLang.startsWith('th')) {
    currentLang = 'th_TH'
  } else if (currentLang.startsWith('vi')) {
    currentLang = 'vi_VN'
  } else {
    currentLang = 'en_US'
  }
  return clientLang(currentLang)
}