import { constantRoutes } from '@/router';
import { getAuthMenu } from '@/api/user';

function filterAsyncRouter(asyncRouterMap) {
  // 遍历后台传来的路由字符串，转换为组件对象
  try {
    const accessedRouters = asyncRouterMap.filter(route => {
      if (route.component) {
        if (route.component === 'Layout') {
          // Layout组件特殊处理
          // route.component = Layout
        } else {
          const component = route.component;
          route.component = resolve => {
            require(['@/views' + component + '.vue'], resolve);
          };
        }
      }
      if (route.children && route.children.length) {
        route.children = filterAsyncRouter(route.children);
      }
      return true;
    });
    return accessedRouters;
  } catch (e) {
    console.log(e);
  }
}

const state = {
  routes: [],
  addRoutes: [],
};

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes;
    state.routes = constantRoutes.concat(routes);
  },
};

const actions = {
  async generateRoutes({ commit }) {
    // 取后台路由
    const asyncRouter = await getAuthMenu();
    //进行封装common
    return new Promise(resolve => {
      const tmp = asyncRouter.data.menu;
      const accessedRoutes = filterAsyncRouter(tmp);
      commit('SET_ROUTES', accessedRoutes);
      resolve(accessedRoutes);
    });
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
