import { publicRoutes } from '@/router/public-routes'
import { privateRoutes } from '@/router/private-routes'
export default {
  namespaced: true,
  state: {
    //   路由表: 初始时所拥有的路由表
    routes: publicRoutes
  },
  mutations: {
    //   增加路由的方法
    setRoutes(state, newRoutes) {
      state.routes = [...publicRoutes, ...newRoutes]
    }
  },
  actions: {
    /**
     * 根据权限数据筛选路由
     * @param {*}  context
     * @param {*}  menus 权限数据
     */
    filterRoutes(context, menus) {
      const routes = []
      menus.forEach((item) => {
        privateRoutes.forEach((key) => {
          if (key.name === item) routes.push(key)
        })
      })
      routes.push({
        path: '/:catchAll(.*)',
        redirect: '/404'
      })

      context.commit('setRoutes', routes)
      return routes
    }
  }
}
