"use client";
import { jsx as _jsx } from "react/jsx-runtime";
import { useCallback, useState } from "react";
import { useCheckPermission } from "../iam-policy/useCheckPermission.js";
import { ManageAccessDialog } from "./ManageAccessDialog.js";
/**
 * Wires the unified Manage access dialog to a kebab/overflow menu — the
 * trigger shape used by static resource detail views (agent, skill,
 * mcp_server, workflow), whose actions live in {@link ResourceActionBar}'s
 * menu rather than as a standalone button.
 *
 * Owns the open-state and the `can_view_access` gate (a viewer may open the
 * dialog to read general access and the people list; the dialog's own sections
 * gate editing). Returns a `null` action when the resource is still loading or
 * the user cannot view access — so the host can unconditionally fold
 * `action` into its actions array.
 *
 * Surfaces that want a *visible* trigger instead use {@link ManageAccessButton}.
 *
 * @example
 * ```tsx
 * const access = useManageAccess({
 *   resource: meta ? { kind: ApiResourceKind.agent, kindString: "agent", id: meta.id, org: meta.org } : null,
 *   visibility: meta ? { kind: "agent", current: meta.visibility, org: meta.org, onChanged: refetch } : undefined,
 * });
 * // ...
 * <ResourceDetailShell actions={access.action ? [...actions, access.action] : actions} ... />
 * {access.dialog}
 * ```
 */
export function useManageAccess({ resource, visibility, extraSection, label = "Manage access", }) {
    const [isOpen, setIsOpen] = useState(false);
    const { allowed: canView } = useCheckPermission(resource ? { kind: resource.kindString, id: resource.id } : null, "can_view_access");
    const open = useCallback(() => setIsOpen(true), []);
    const action = resource && canView
        ? { id: "manage-access", label, group: "sharing", onAction: open }
        : null;
    const dialog = resource ? (_jsx(ManageAccessDialog, { open: isOpen, onOpenChange: setIsOpen, resource: resource, visibility: visibility, extraSection: extraSection })) : null;
    return { action, dialog, open, isOpen };
}
//# sourceMappingURL=useManageAccess.js.map