// src/lib/has-role-permission.ts
import { grant } from "@mesob/common";
import { HTTPException } from "hono/http-exception";
var toArray = (v) => {
  return Array.isArray(v) ? v : [v];
};
var hasRole = (c, role) => {
  const user = c.get("user");
  const codes = user?.roleCodes;
  if (!codes?.length) {
    return false;
  }
  const check = toArray(role);
  return check.some((r) => codes.includes(r));
};
var hasRoleThrow = (c, role) => {
  if (!hasRole(c, role)) {
    throw new HTTPException(401, { message: "Unauthorized" });
  }
};
var hasPermission = (c, permission) => {
  const user = c.get("user");
  const perms = user?.permissions;
  const check = toArray(permission);
  return grant(check, perms);
};
var hasPermissionThrow = (c, permission) => {
  if (!hasPermission(c, permission)) {
    throw new HTTPException(401, { message: "Unauthorized" });
  }
};
export {
  hasPermission,
  hasPermissionThrow,
  hasRole,
  hasRoleThrow
};
//# sourceMappingURL=has-role-permission.js.map