import { computed } from "vue";
import { btnList } from "/@/config/btnList";
import { usePermissionStoreHook } from "../store/modules/permission";

/**
 * 按钮鉴权
 * @param {menuRoute} 权限路由
 */
export function usePermission(menuRoute?: string, processType?: string) {
  const { powers, proPermissions: _proPermissions } = usePermissionStoreHook();

  const permissions = computed(() => powers.get(menuRoute) || []);

  const proPermissions = computed(() => _proPermissions.get(processType) || []);

  function hasPermissionWithName(permissionName: string) {
    const permission = btnList.find(({ name }) => permissionName === name);
    const isHas = permissions.value.includes(permission.code);
    return isHas;
  }

  function hasPermissionWithCode(permissionCode: string) {
    console.log(permissions.value);
    const isHas = permissions.value.includes(permissionCode);
    return isHas;
  }

  function hasPermissionWithCodes(permissionCodes: string[]) {
    const isHas = permissions.value.every(p => permissionCodes.includes(p));
    return isHas;
  }

  return {
    permissions,
    proPermissions,
    hasPermissionWithName,
    hasPermissionWithCode,
    hasPermissionWithCodes
  };
}
