"use client";
import { Fragment, jsx } from "react/jsx-runtime";
import { usePermissions } from "../../lib/permissions/hooks.js";
function PermissionGate({
  permission,
  permissions,
  anyPermission,
  children,
  fallback = null,
  showFallback = true
}) {
  const permissionsToCheck = {};
  if (permission) {
    permissionsToCheck["single"] = permission;
  }
  if (permissions) {
    permissions.forEach((p, i) => {
      permissionsToCheck[`and_${i}`] = p;
    });
  }
  if (anyPermission) {
    anyPermission.forEach((p, i) => {
      permissionsToCheck[`or_${i}`] = p;
    });
  }
  const results = usePermissions(permissionsToCheck);
  let hasAccess = false;
  if (permission) {
    hasAccess = results["single"];
  } else if (permissions) {
    hasAccess = permissions.every((_, i) => results[`and_${i}`]);
  } else if (anyPermission) {
    hasAccess = anyPermission.some((_, i) => results[`or_${i}`]);
  }
  if (hasAccess) {
    return /* @__PURE__ */ jsx(Fragment, { children });
  }
  if (showFallback && fallback) {
    return /* @__PURE__ */ jsx(Fragment, { children: fallback });
  }
  return null;
}
function withPermission(WrappedComponent, permission, FallbackComponent) {
  return function WithPermissionWrapper(props) {
    return /* @__PURE__ */ jsx(
      PermissionGate,
      {
        permission,
        fallback: FallbackComponent ? /* @__PURE__ */ jsx(FallbackComponent, {}) : null,
        children: /* @__PURE__ */ jsx(WrappedComponent, { ...props })
      }
    );
  };
}
export {
  PermissionGate,
  withPermission
};
