/**
 * @description 路由拦截状态管理
 */

import { asyncRoutes, constantRoutes, resetRouter } from '@/router';
import { filterRoutes } from '@/utils/routes';
import { rolesControl } from '@/config';

const state = () => ({
  routes: [],
  partialRoutes: [],
});
const getters = {
  routes: state => state.routes,
  partialRoutes: state => state.partialRoutes,
};
const mutations = {
  setRoutes(state, routes) {
    state.routes = routes;
  },
};
const actions = {
  /**
   * @description 多模式设置路由(默认前端路由,未使用模式可删除相应代码即可)
   * @param {*} { commit }
   * @param mode
   * @returns
   */
  async setRoutes({ commit }, mode = 'none') {
    // 默认前端路由
    const routes = [...asyncRoutes];
    // 设置游客路由关闭路由拦截(不需要可以删除)
    const control = mode === 'visit' ? false : rolesControl;
    // 根据权限和rolesControl过滤路由
    const finallyRoutes = filterRoutes([...constantRoutes, ...routes], control);
    // 设置菜单所需路由
    commit('setRoutes', finallyRoutes);
    // 根据可访问路由重置Vue Router
    await resetRouter(finallyRoutes);
  },
};
export default { state, getters, mutations, actions };
