import { routes } from "./routes";

// 修改此函数, 自定义判断是否具有权限
function hasPermission(route, permissions) {
  const not = route.meta && route.meta.permission && permissions.indexOf(route.meta.permission) < 0;
  return !not;
}

let authedRoutes = [];

export function getAuthedRoutes() {
  return authedRoutes;
}

export const refreshAuthedRoutes = (permissions) => {
  const get = (rts) => rts.map(route => {
    if (hasPermission(route, permissions)) {
      if (route.children) {
        route.children = get(route.children);
      }
  
      return route;
    }

    return false;
  }).filter(Boolean);

  authedRoutes = get(routes);
}
