import { useCallback, useState } from "react";
import { approveWithDeleteAllowance, resolvePendingToolCalls, switchToAutonomousAndApprove, } from "./approvalActions";
/**
 * State wrapper around approvalActions for the approval card: one busy action
 * at a time, and an inline error message the user can retry from.
 */
export function useApprovalActions(params) {
    const { agentId, readOnly, onSwitchToAutonomous } = params;
    const [busyAction, setBusyAction] = useState(null);
    const [error, setError] = useState(null);
    const clearError = useCallback(() => setError(null), []);
    const run = useCallback(async (action, failurePrefix, fn) => {
        if (readOnly || !agentId || busyAction !== null)
            return;
        setError(null);
        setBusyAction(action);
        try {
            await fn();
        }
        catch (err) {
            console.error(`${failurePrefix}:`, err);
            setError(`${failurePrefix}: ${err?.message || "Unknown error"}. Try again.`);
        }
        finally {
            setBusyAction(null);
        }
    }, [agentId, busyAction, readOnly]);
    const approveAll = useCallback((pending) => run("approve", "Failed to approve", () => resolvePendingToolCalls({ agentId: agentId, pending, approved: true })), [agentId, run]);
    const rejectAll = useCallback((pending) => run("reject", "Failed to reject", () => resolvePendingToolCalls({
        agentId: agentId,
        pending,
        approved: false,
    })), [agentId, run]);
    const switchToAutonomous = useCallback((pending) => run("autonomous", "Failed to switch to autonomous mode", () => switchToAutonomousAndApprove({
        agentId: agentId,
        pending,
        onSwitched: onSwitchToAutonomous,
    })), [agentId, onSwitchToAutonomous, run]);
    const approveWithAllowance = useCallback((item) => {
        if (!item.allowableScope)
            return Promise.resolve();
        return run({ type: "allowance", toolCallId: item.toolCallId }, "Failed to allow deletes in scope", () => approveWithDeleteAllowance({
            agentId: agentId,
            pending: item,
            allowableScope: item.allowableScope,
        }));
    }, [agentId, run]);
    return {
        busyAction,
        error,
        clearError,
        approveAll,
        rejectAll,
        switchToAutonomous,
        approveWithAllowance,
    };
}
//# sourceMappingURL=useApprovalActions.js.map