"use client";
import { useCallback, useMemo, useRef } from "react";
import { create } from "@bufbuild/protobuf";
import { IamPolicySpecSchema, ApiResourceRefSchema, } from "@stigmer/protos/ai/stigmer/iam/iampolicy/v1/spec_pb";
import { useResourceAccess } from "./useResourceAccess.js";
import { useCreateIamPolicy } from "./useCreateIamPolicy.js";
import { useDeleteIamPolicy } from "./useDeleteIamPolicy.js";
import { useGrantableRoles } from "./useGrantableRoles.js";
/**
 * Behavior hook that orchestrates the share flow for any resource.
 *
 * Composes {@link useResourceAccess}, {@link useCreateIamPolicy}, and
 * {@link useDeleteIamPolicy} into a single cohesive API suitable for
 * powering a share dialog.
 *
 * Pass `null` as `resource` to produce a stable no-op (useful while
 * the resource hasn't loaded yet).
 *
 * @param resource - The resource to share, or `null` to skip.
 *
 * @example
 * ```tsx
 * const share = useShareFlow({
 *   kind: "session",
 *   id: sessionId,
 *   resourceKind: ApiResourceKind.session,
 * });
 *
 * // Render access list
 * share.accessList.map(entry => ...);
 *
 * // Grant viewer access
 * await share.grantAccess(userId, "viewer");
 * ```
 */
export function useShareFlow(resource) {
    const accessRef = useMemo(() => (resource ? { kind: resource.kind, id: resource.id } : null), [resource?.kind, resource?.id]);
    const { members: accessList, isLoading, isRefetching, error: fetchError, refetch, } = useResourceAccess(accessRef);
    const { roles: grantableRoles, hasRoles: hasGrantableRoles } = useGrantableRoles(resource?.resourceKind ?? null);
    const { create: createPolicy, isCreating: isGranting, error: grantError, clearError: clearGrantError } = useCreateIamPolicy();
    const { remove: removePolicy, isDeleting: isRevoking, error: revokeError, clearError: clearRevokeError } = useDeleteIamPolicy();
    const resourceRef = useRef(resource);
    resourceRef.current = resource;
    const grantAccess = useCallback(async (principalId, role) => {
        const res = resourceRef.current;
        if (!res)
            return;
        const spec = create(IamPolicySpecSchema, {
            principal: create(ApiResourceRefSchema, {
                kind: "identity_account",
                id: principalId,
            }),
            resource: create(ApiResourceRefSchema, {
                kind: res.kind,
                id: res.id,
            }),
            relation: role,
        });
        await createPolicy(spec);
        refetch();
    }, [createPolicy, refetch]);
    const revokeAccess = useCallback(async (principalId, role) => {
        const res = resourceRef.current;
        if (!res)
            return;
        const spec = create(IamPolicySpecSchema, {
            principal: create(ApiResourceRefSchema, {
                kind: "identity_account",
                id: principalId,
            }),
            resource: create(ApiResourceRefSchema, {
                kind: res.kind,
                id: res.id,
            }),
            relation: role,
        });
        await removePolicy(spec);
        refetch();
    }, [removePolicy, refetch]);
    const clearErrors = useCallback(() => {
        clearGrantError();
        clearRevokeError();
    }, [clearGrantError, clearRevokeError]);
    return {
        accessList,
        isLoading,
        isRefetching,
        fetchError,
        grantableRoles,
        hasGrantableRoles,
        grantAccess,
        isGranting,
        grantError,
        revokeAccess,
        isRevoking,
        revokeError,
        refetch,
        clearErrors,
    };
}
//# sourceMappingURL=useShareFlow.js.map