"use client";
import { useCallback, useState } from "react";
import { create } from "@bufbuild/protobuf";
import { SubmitApprovalInputSchema } from "@stigmer/protos/ai/stigmer/agentic/agentexecution/v1/io_pb";
import { useStigmer } from "../hooks";
import { toError } from "../internal/toError";
/**
 * Behavior hook that wraps `agentExecution.submitApproval()` with
 * per-tool-call loading state and error management.
 *
 * Tracks submitting state per tool call ID (not a single boolean)
 * because batch approvals allow concurrent decisions on different
 * tool calls within the same execution.
 *
 * Platform builders who need full control over the approval UI use
 * this hook directly. Those who prefer drop-in components use
 * {@link ApprovalCard} which accepts the callback shape produced
 * by this hook.
 *
 * @example
 * ```tsx
 * const { submitApproval, submittingToolCallIds, error } = useSubmitApproval();
 *
 * await submitApproval(executionId, toolCallId, ApprovalAction.APPROVAL_ACTION_APPROVE);
 * ```
 */
export function useSubmitApproval() {
    const stigmer = useStigmer();
    const [submittingIds, setSubmittingIds] = useState(new Set());
    const [error, setError] = useState(null);
    const clearError = useCallback(() => setError(null), []);
    const submitApproval = useCallback(async (executionId, toolCallId, action, comment) => {
        setSubmittingIds((prev) => {
            const next = new Set(prev);
            next.add(toolCallId);
            return next;
        });
        setError(null);
        try {
            const input = create(SubmitApprovalInputSchema, {
                agentExecutionId: executionId,
                toolCallId,
                action,
                comment: comment ?? "",
            });
            await stigmer.agentExecution.submitApproval(input);
        }
        catch (err) {
            setError(toError(err));
            throw err;
        }
        finally {
            setSubmittingIds((prev) => {
                const next = new Set(prev);
                next.delete(toolCallId);
                return next;
            });
        }
    }, [stigmer]);
    return { submitApproval, submittingToolCallIds: submittingIds, error, clearError };
}
//# sourceMappingURL=useSubmitApproval.js.map