import { changSaveLocal, getLocal } from "@/util/localUtil";
import { ref } from "vue";
import { findRoute, RouteName, Routes } from "@/router";
import { pubPermissions } from "@/constant/permission";
import { DataScope, RoleLevel, RoleMenuTreeItem, getRole } from "./role";

export type UserPermission = {
  resourceKey: string;
  dataScope: DataScope;
  type: "menu" | "button";
};

export const permission = ref(getLocal("permission", [] as UserPermission[]));

changSaveLocal("permission", () => permission.value);

/**
 * 获取具有权限的路由
 * @param routeNames 所有路由
 */
export const getPermissionRoutes = (routeNames: string[]) => {
  console.error(permission.value);
  console.error(pubPermissions,routeNames, 'routeNames');
  // .filter((routeName) =>
  //   permission.value.some((p) => p.resourceKey === routeName)
  // )
  return routeNames
    .map((routeName) => findRoute(routeName))
    .filter((route) => route) as Routes;
};

export const setPermission = (perms: UserPermission[]) => {
  permission.value = perms.concat(pubPermissions);
};
