import antdLocale from "ant-design-vue/es/locale/zh_CN";
import { set } from "lodash-es";

import routesAccount from "./zh-CN/routes/account.js";
import routesBasic from "./zh-CN/routes/basic.js";
import routesDashboard from "./zh-CN/routes/dashboard.js";
import routesDemo from "./zh-CN/routes/demo.js";
import common from "./zh-CN/common.js";
import component from "./zh-CN/component.js";
import layout from "./zh-CN/layout.js";

/** localeDir：与路径首段一致（如 zh-CN），须剥离否则文案会挂在 zh-CN.component 下，导致 t('component.xxx') 无法命中 */
function genMessage(langs, localeDir) {
  const obj = {};
  langs.keys().forEach((key) => {
    const langFileModule = langs(key).default;
    // 移除开头的 ./
    let fileName = key.replace(/^\.\//, "");
    const lastIndex = fileName.lastIndexOf(".");
    fileName = fileName.substring(0, lastIndex);
    const keyList = fileName.split("/");
    if (keyList[0] === localeDir) {
      keyList.shift();
    }
    const moduleName = keyList.shift();
    const objKey = keyList.join(".");

    if (moduleName) {
      if (objKey) {
        set(obj, moduleName, obj[moduleName] || {});
        set(obj[moduleName], objKey, langFileModule);
      } else {
        set(obj, moduleName, langFileModule || {});
      }
    }
  });
  return obj;
}

const modulesFiles = (key) => {
  const map = {
    "./zh-CN/common.js": common,
    "./zh-CN/component.js": component,
    "./zh-CN/layout.js": layout,
    "./zh-CN/routes/account.js": routesAccount,
    "./zh-CN/routes/basic.js": routesBasic,
    "./zh-CN/routes/dashboard.js": routesDashboard,
    "./zh-CN/routes/demo.js": routesDemo,
  };
  return { default: map[key] };
};
modulesFiles.keys = () => [
  "./zh-CN/common.js",
  "./zh-CN/component.js",
  "./zh-CN/layout.js",
  "./zh-CN/routes/account.js",
  "./zh-CN/routes/basic.js",
  "./zh-CN/routes/dashboard.js",
  "./zh-CN/routes/demo.js",
];

export default {
  message: {
    ...genMessage(modulesFiles, "zh-CN"),
    antdLocale,
  },
};
