export default {
  namespaced: true,
  state: {
    isInvalid: false, // 判断token是否失效
    isReset: false, // 判断重置密码
    prevUserId: null, // 上一个登录用户id
    userId: null, // 当前登录用户id
    tenantId: undefined, // 租户id
    favoritesMenu: [],
    mostUsed: [],
    menus: [],
    permissions: [],
  },
  getters: {
    menuMap(state) {
      // 菜单删除按钮层 处理
      const fn = (data = [], res = []) => {
        data.forEach((item, index) => {
          const { children, ...rest } = item;
          res.push({ ...rest });
          if (children?.length) {
            if (children.every((e) => e.menuType !== "BUTTON")) {
              res[index].children = [];
              fn(children, res[index].children);
            }
          }
        });
        return res;
      };
      // 菜单Map处理
      const setMap = (data = [], map = new Map()) => {
        data?.forEach((item) => {
          const children = item?.children || [];
          map.set(item.code, children);
          if (!children.some((e) => ["MENU", "BUTTON"].includes(e.menuType))) {
            setMap(children, map);
          }
        });
        return map;
      };

      return setMap(fn(state.menus));
    },
  },
  mutations: {
    SET_FAVORITE_MENUS(state, payload) {
      // eslint-disable-next-line
      state.favoritesMenu = payload;
    },
    SET_MOST_USED(state, payload) {
      // eslint-disable-next-line
      state.mostUsed = payload;
    },
    // 菜单
    SET_MENUS(state, payload) {
      // eslint-disable-next-line
      state.menus = payload;
    },
    // 权限
    SET_PERMISSIONS(state, payload) {
      // eslint-disable-next-line
      state.permissions = payload;
    },
    // 是否显示登录框
    SET_IS_INVALID(state, payload) {
      // eslint-disable-next-line
      state.isInvalid = payload;
    },
    // 重置密码
    SET_IS_RESET(state, payload) {
      // eslint-disable-next-line
      state.isReset = payload;
    },
    // 上一个登录用户id
    SET_PERV_USER_ID(state, payload) {
      // eslint-disable-next-line
      state.prevUserId = payload;
    },
    // 当前登录用户id
    SET_USER_ID(state, payload) {
      // eslint-disable-next-line
      state.userId = payload;
    },
    // 当前登录租户id
    SET_TENANT_ID(state, payload) {
      // eslint-disable-next-line
      state.tenantId = payload;
    },
  },
  actions: {
    getUserMenuInfo(
      { commit },
      payload = {
        menus: [],
        permissions: [],
      }
    ) {
      commit("SET_MENUS", payload.menus);
      commit("SET_PERMISSIONS", payload.permissions);
    },
  },
};
