import { createContext, useCallback, useContext } from "react";
const DEFAULT_VALUE = {
    approvalsByToolCallId: new Map(),
    submittingIds: new Set(),
    errorsByToolCallId: new Map(),
};
/** Context that supplies a run's pending approvals to the tool rows they gate. */
export const ApprovalContext = createContext(DEFAULT_VALUE);
/**
 * Returns the inline-approval state for a tool call, or `null` when the call
 * is not currently gated (no matching approval, or no submit handler wired).
 *
 * The returned `onSubmit` is pre-bound to `toolCallId` and stable across
 * renders, so it can flow straight into a memoized approval body.
 *
 * @example
 * ```tsx
 * const approval = useApproval(toolCall.id);
 * if (approval) {
 *   return <ApprovalCardBody {...approval} />;
 * }
 * ```
 */
export function useApproval(toolCallId) {
    const { approvalsByToolCallId, onSubmit, submittingIds, errorsByToolCallId } = useContext(ApprovalContext);
    const handleSubmit = useCallback((action, comment) => {
        onSubmit?.(toolCallId, action, comment);
    }, [onSubmit, toolCallId]);
    const pendingApproval = toolCallId
        ? approvalsByToolCallId.get(toolCallId)
        : undefined;
    if (!pendingApproval || !onSubmit)
        return null;
    return {
        pendingApproval,
        onSubmit: handleSubmit,
        isSubmitting: submittingIds.has(toolCallId),
        error: errorsByToolCallId.get(toolCallId) ?? null,
    };
}
//# sourceMappingURL=ApprovalContext.js.map