import { onMounted, ref } from "vue";
import { btnList } from "/@/config/btnList";
import { useUserStoreHook } from "/@/store/modules/user";

/**
 * 按钮鉴权
 * @param {menuRoute} 权限路由
 */
export function usePermission(pageName: string) {
  const permissions = ref<string[]>([]);

  function hasPermissionWithName(permissionName: string) {

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

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

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

  onMounted(
    () => {
      permissions.value = useUserStoreHook().getMenuActions(pageName)
    }
  );

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