"use strict";
const common_vendor = require("../common/vendor.js");
const utils_storage = require("../utils/storage.js");
const utils_constant = require("../utils/constant.js");
const utils_request = require("../utils/request.js");
const usePermissionStore = common_vendor.defineStore("permission", {
  state: () => ({
    permissions_menu: utils_storage.storage.get(utils_constant.constant.permissions_menu),
    permissions_btn: utils_storage.storage.get(utils_constant.constant.permissions_btn)
  }),
  actions: {
    // 获取用户权限信息
    GetPermis() {
      return new Promise(async (resolve, reject) => {
        let sysIds = await getSystemMenus();
        if (sysIds.length > 0) {
          utils_request.request({
            url: "/authorize/sys-menu/workMenuList",
            data: {
              sysId: sysIds
              //平台端获取固定系统ID
            }
          }).then((res) => {
            if (res && res.length > 0) {
              const sdata = JSON.parse(JSON.stringify(res));
              let assembleData = refitRouterList(sdata);
              this.permissions_btn = assembleData.btn;
              utils_storage.storage.set(utils_constant.constant.permissions_btn, assembleData.btn);
              this.permissions_menu = assembleData.tree;
              utils_storage.storage.set(
                utils_constant.constant.permissions_menu,
                this.permissions_menu
              );
            }
            resolve(res);
          }).catch((error) => {
            reject(error);
          });
        } else {
          reject("没有权限");
        }
      });
    }
  }
});
function getSystemMenus() {
  return new Promise((resolve, reject) => {
    utils_request.request({
      url: "/authorize/sys-system/list",
      data: {}
    }).then((res) => {
      console.log("getSystemMenus", res);
      let arr = [];
      let menuCodes = ["xy_system", "xy_quality", "xy_data", "xy_merc_be"];
      if (res && res.length) {
        res.forEach((i) => {
          if (menuCodes.find((j) => j == i.code)) {
            arr.push(i.id);
          }
        });
      }
      resolve(arr);
    }).catch((err) => {
      reject(err);
    });
  });
}
function refitRouterList(allRouterMenu) {
  let newRouterList = new Array();
  let btnPromission = new Array();
  for (let index = 0; index < allRouterMenu.length; index++) {
    const element = allRouterMenu[index];
    let newSystemItem = adjustRouterList(element, true);
    if (element.menu && element.menu.length > 0) {
      let newMenuList = new Array();
      let menuList = element.menu;
      for (let index2 = 0; index2 < menuList.length; index2++) {
        const router = menuList[index2];
        let newRouterItem = adjustRouterList(router);
        newMenuList.push(newRouterItem);
        if (router.type !== 1) {
          btnPromission.push(router.code);
        }
      }
      newSystemItem.children = assembleRouter(newMenuList);
    }
    newRouterList.push(newSystemItem);
  }
  return {
    tree: newRouterList,
    btn: btnPromission
  };
}
function adjustRouterList(routerItem, isSystemMenu = false) {
  let newRouterItem = {};
  if (isSystemMenu) {
    newRouterItem = {
      id: routerItem.id,
      name: routerItem.code,
      hidden: false,
      meta: {
        title: routerItem.name,
        icon: routerItem.icon
      },
      path: "/" + routerItem.code,
      //路由路径
      component: "Layout",
      //组件地址
      children: []
    };
    return newRouterItem;
  }
  newRouterItem.name = routerItem.code;
  newRouterItem.id = routerItem.id;
  newRouterItem.paterId = routerItem.paterId;
  newRouterItem.hidden = false;
  newRouterItem.type = routerItem.type;
  newRouterItem.meta = {
    icon: routerItem.icon,
    title: routerItem.name
  };
  if (routerItem.paterId === null && routerItem.type != 2 || routerItem.type == 1) {
    newRouterItem.path = "/" + routerItem.route;
    newRouterItem.component = "ParentView";
  }
  if (routerItem.type == 2) {
    newRouterItem.path = "/" + routerItem.route;
    newRouterItem.component = routerItem.route;
    newRouterItem.code = routerItem.code;
  }
  if (routerItem.type == 3) {
    newRouterItem = routerItem;
  }
  return newRouterItem;
}
function assembleRouter(allRouterMenu) {
  const idMapping = allRouterMenu.reduce((acc, el, i) => {
    acc[el.id] = i;
    return acc;
  }, {});
  let root = [];
  allRouterMenu.forEach((el) => {
    if (el.paterId === null || el.paterId === 0) {
      root.push(el);
      return;
    }
    const parentEl = allRouterMenu[idMapping[el.paterId]];
    if (el.type !== 3) {
      parentEl.children = [...parentEl.children || [], el];
    }
  });
  return root;
}
exports.usePermissionStore = usePermissionStore;
