import { Component } from "@contember/binding";
import { identityEnvironmentExtension } from "./IdentityEnvironmentExtension.js";
import { projectEnvironmentExtension } from "../Project/ProjectEnvironmentExtension.js";
import { jsx, Fragment } from "react/jsx-runtime";
import { useProjectUserRoles } from "../../acl/useProjectUserRoles.js";
const HasRole = Component(({
  children,
  role
}) => {
  const projectRoles = useProjectUserRoles();
  return evaluateCondition(role, projectRoles) ? /* @__PURE__ */ jsx(Fragment, {
    children
  }) : null;
}, ({
  children,
  role
}, env) => {
  const identity = env.getExtension(identityEnvironmentExtension).identity;
  const project = env.getExtension(projectEnvironmentExtension).slug;
  const projectRoles = new Set(identity?.projects.find((it) => it.slug === project)?.roles ?? []);
  return evaluateCondition(role, projectRoles) ? /* @__PURE__ */ jsx(Fragment, {
    children
  }) : null;
});
const evaluateCondition = (condition, roles) => {
  if (typeof condition === "string") {
    return roles.has(condition);
  }
  return condition(roles);
};
export {
  HasRole
};
//# sourceMappingURL=HasRole.js.map
