import { createI18n } from 'vue-i18n';
import de from '../../../node_modules/.pnpm/element-plus@2.13.1_vue@3.5.26_typescript@5.8.3_/node_modules/element-plus/es/locale/lang/de.mjs';
import English from '../../../node_modules/.pnpm/element-plus@2.13.1_vue@3.5.26_typescript@5.8.3_/node_modules/element-plus/es/locale/lang/en.mjs';
import es from '../../../node_modules/.pnpm/element-plus@2.13.1_vue@3.5.26_typescript@5.8.3_/node_modules/element-plus/es/locale/lang/es.mjs';
import fi from '../../../node_modules/.pnpm/element-plus@2.13.1_vue@3.5.26_typescript@5.8.3_/node_modules/element-plus/es/locale/lang/fi.mjs';
import fr from '../../../node_modules/.pnpm/element-plus@2.13.1_vue@3.5.26_typescript@5.8.3_/node_modules/element-plus/es/locale/lang/fr.mjs';
import it from '../../../node_modules/.pnpm/element-plus@2.13.1_vue@3.5.26_typescript@5.8.3_/node_modules/element-plus/es/locale/lang/it.mjs';
import ja from '../../../node_modules/.pnpm/element-plus@2.13.1_vue@3.5.26_typescript@5.8.3_/node_modules/element-plus/es/locale/lang/ja.mjs';
import ko from '../../../node_modules/.pnpm/element-plus@2.13.1_vue@3.5.26_typescript@5.8.3_/node_modules/element-plus/es/locale/lang/ko.mjs';
import no from '../../../node_modules/.pnpm/element-plus@2.13.1_vue@3.5.26_typescript@5.8.3_/node_modules/element-plus/es/locale/lang/no.mjs';
import pl from '../../../node_modules/.pnpm/element-plus@2.13.1_vue@3.5.26_typescript@5.8.3_/node_modules/element-plus/es/locale/lang/pl.mjs';
import pt from '../../../node_modules/.pnpm/element-plus@2.13.1_vue@3.5.26_typescript@5.8.3_/node_modules/element-plus/es/locale/lang/pt.mjs';
import ru from '../../../node_modules/.pnpm/element-plus@2.13.1_vue@3.5.26_typescript@5.8.3_/node_modules/element-plus/es/locale/lang/ru.mjs';
import zhCn from '../../../node_modules/.pnpm/element-plus@2.13.1_vue@3.5.26_typescript@5.8.3_/node_modules/element-plus/es/locale/lang/zh-cn.mjs';
import zhTw from '../../../node_modules/.pnpm/element-plus@2.13.1_vue@3.5.26_typescript@5.8.3_/node_modules/element-plus/es/locale/lang/zh-tw.mjs';
import th from '../../../node_modules/.pnpm/element-plus@2.13.1_vue@3.5.26_typescript@5.8.3_/node_modules/element-plus/es/locale/lang/th.mjs';
import id from '../../../node_modules/.pnpm/element-plus@2.13.1_vue@3.5.26_typescript@5.8.3_/node_modules/element-plus/es/locale/lang/id.mjs';
import ms from '../../../node_modules/.pnpm/element-plus@2.13.1_vue@3.5.26_typescript@5.8.3_/node_modules/element-plus/es/locale/lang/ms.mjs';
import vi from '../../../node_modules/.pnpm/element-plus@2.13.1_vue@3.5.26_typescript@5.8.3_/node_modules/element-plus/es/locale/lang/vi.mjs';
import msMY from '../../../_virtual/en-US.mjs';
import deDE from '../../../_virtual/de-DE.mjs';
import esES from '../../../_virtual/es-ES.mjs';
import frFR from '../../../_virtual/fr-FR.mjs';
import itIT from '../../../_virtual/it-IT.mjs';
import jaJP from '../../../_virtual/ja-JP.mjs';
import koKR from '../../../_virtual/ko-KR.mjs';
import noNO from '../../../_virtual/nb-NO.mjs';
import ptBR from '../../../_virtual/pt-BR.mjs';
import ruRU from '../../../_virtual/ru-RU.mjs';
import zhCN from '../../../_virtual/zh-CN.mjs';
import zhHK from '../../../_virtual/zh-HK.mjs';
import zhTW from '../../../_virtual/zh-TW.mjs';
import thTH from '../../../_virtual/th-TH.mjs';
import viVN from '../../../_virtual/vi-VN.mjs';

const messages = {};
const element = {
  "zh-CN": zhCn,
  "zh-TW": zhTw,
  "zh-HK": zhTw,
  en: English,
  de: de,
  es: es,
  fi: fi,
  fr: fr,
  it: it,
  ja: ja,
  ko: ko,
  no: no,
  pl: pl,
  pt: pt,
  ru: ru,
  th: th,
  id: id,
  ms: ms,
  vi: vi
};
const vxe = {
  "zh-CN": zhCN,
  "zh-TW": zhTW,
  "zh-HK": zhHK,
  en: msMY,
  de: deDE,
  es: esES,
  fi: msMY,
  fr: frFR,
  it: itIT,
  ja: jaJP,
  ko: koKR,
  no: noNO,
  pl: msMY,
  pt: ptBR,
  ru: ruRU,
  th: thTH,
  id: msMY,
  ms: msMY,
  vi: viVN
};
const languageList = {
  "zh-CN": "\u7B80\u4F53\u4E2D\u6587",
  "zh-TW": "\u7E41\u9AD4\u4E2D\u6587(\u53F0\u7063)",
  "zh-HK": "\u7E41\u9AD4\u4E2D\u6587(\u9999\u6E2F)",
  en: "English",
  de: "Deutsch",
  es: "Espa\xF1ol",
  fi: "Suomeksi",
  fr: "Fran\xE7ais",
  it: "Italiano",
  ja: "\u65E5\u672C\u8A9E",
  ko: "\uD55C\uAD6D\uC5B4",
  no: "Norsk",
  pl: "Polski",
  pt: "Portugu\xEAs",
  ru: "\u0420\u0443\u0441\u0441\u043A\u0438\u0439",
  th: "\u0E44\u0E17\u0E22",
  id: "Indonesia",
  ms: "Malaysia",
  vi: "Vi\u1EC7t Nam"
};
const iso_3166_1_CountryList = {
  de: "de",
  en: "us",
  es: "es",
  fi: "fi",
  fr: "fr",
  it: "it",
  ja: "jp",
  ko: "kr",
  no: "no",
  pl: "pl",
  pt: "pt",
  ru: "ru",
  "zh-CN": "cn",
  "zh-TW": "tw",
  "zh-HK": "hk",
  th: "th",
  id: "id",
  ms: "ms",
  vi: "vi"
};
const itemize = { en: [], "zh-CN": [], "zh-TW": [], "zh-HK": [], de: [], es: [], fi: [], fr: [], it: [], ja: [], ko: [], no: [], pl: [], pt: [], ru: [], th: [], id: [], ms: [], vi: [] };
const getCountryCode = (locale) => {
  return iso_3166_1_CountryList[locale];
};
function mergeArrObj(list, key) {
  let obj = {};
  list[key].forEach((i) => {
    obj = Object.assign({}, obj, i);
  });
  return obj;
}
const i18n = createI18n({
  legacy: false,
  silentTranslationWarn: true,
  missingWarn: false,
  silentFallbackWarn: true,
  fallbackWarn: false,
  locale: "zh-CN",
  fallbackLocale: "zh-CN",
  messages: {},
  globalInjection: true,
  fallbackFormat: false,
  // 关闭默认的 fallback 行为
  missing: (locale, key) => {
    const lastPart = key.includes(".") ? key.split(".").pop() : key;
    return lastPart.replace(/\${(.+?)}/g, "$1");
  }
});
async function mergMessage(lang, msg) {
  i18n.global.mergeLocaleMessage(lang, {
    "\u4F60\u597D\u5417": "\u4F60\u597D\u5417\uFF1F",
    "\u6765\u5566": "\u5566\u5566\u5566"
  });
}
async function loadLocaleMessages(tempmodel) {
  for (const path in tempmodel) {
    const key = path.match(/(\S+)\/(\S+).ts/);
    if (itemize[key[2]]) itemize[key[2]].push(tempmodel[path].default);
    else itemize[key[2]] = tempmodel[path];
  }
  for (const key in itemize) {
    messages[key] = {
      name: key,
      el: element[key].el,
      message: mergeArrObj(itemize, key),
      vxe: vxe[key].vxe
    };
    i18n.global.setLocaleMessage(key, messages[key]);
  }
}
async function setupI18n(app, tempmodel, lang) {
  await loadLocaleMessages(tempmodel);
  i18n.global.locale.value = lang;
  app.use(i18n);
  i18n.global.setMissingHandler((locale, key) => {
    if (key.includes(".")) {
      console.warn(
        `[intlify] Not found '${key}' key in '${locale}' locale messages.`
      );
    }
  });
}

export { getCountryCode, i18n, iso_3166_1_CountryList, languageList, mergMessage, setupI18n };
