"use client";
import { useCallback, useMemo, useState } from "react";
import { create } from "@bufbuild/protobuf";
import { FileDecisionScope, } from "@stigmer/protos/ai/stigmer/agentic/agentexecution/v1/enum_pb";
import { SubmitFileDecisionInputSchema } from "@stigmer/protos/ai/stigmer/agentic/agentexecution/v1/io_pb";
import { useStigmer } from "../hooks.js";
import { toError } from "../internal/toError.js";
/**
 * The loading-state key for a file decision: the change set id for a whole-set
 * (CHANGE_SET) decision, or `changeSetId:fileChangeId` for a per-file one.
 */
export function fileDecisionKey(changeSetId, fileChangeId) {
    return fileChangeId ? `${changeSetId}:${fileChangeId}` : changeSetId;
}
/** A stable empty map so an error-free hook keeps a constant `decisionErrors` ref. */
const NO_DECISION_ERRORS = new Map();
/**
 * Behavior hook that wraps `agentExecution.submitFileDecision()` with per-decision
 * loading state and error management — the file-review sibling of
 * {@link useSubmitApproval}.
 *
 * File edits are reviewed as a captured change set (the `file_change_sets`
 * projection), NOT as tool-call approvals. Platform builders who want full
 * control over the review UI use this hook directly; those who prefer a drop-in
 * surface use {@link FileReviewCard}, which accepts the callback shape this
 * hook produces.
 *
 * @example
 * ```tsx
 * const { submitFileDecision, submittingDecisionKeys } = useFileReview();
 *
 * // Approve the whole change set:
 * await submitFileDecision(executionId, changeSet.id, FileDecisionAction.APPROVE, {
 *   expectedDigest: changeSet.aggregateDigest,
 * });
 * ```
 */
export function useFileReview() {
    const stigmer = useStigmer();
    const [submittingKeys, setSubmittingKeys] = useState(new Set());
    const [decisionErrors, setDecisionErrors] = useState(NO_DECISION_ERRORS);
    const [error, setError] = useState(null);
    const clearDecisionError = useCallback((key) => {
        setDecisionErrors((prev) => {
            if (!prev.has(key))
                return prev;
            const next = new Map(prev);
            next.delete(key);
            return next;
        });
    }, []);
    const clearError = useCallback(() => {
        setDecisionErrors(NO_DECISION_ERRORS);
        setError(null);
    }, []);
    const submitFileDecision = useCallback(async (executionId, changeSetId, action, options) => {
        const fileChangeId = options?.fileChangeId ?? "";
        const scope = options?.scope ??
            (fileChangeId ? FileDecisionScope.FILE : FileDecisionScope.CHANGE_SET);
        const key = fileDecisionKey(changeSetId, fileChangeId || undefined);
        setSubmittingKeys((prev) => {
            const next = new Set(prev);
            next.add(key);
            return next;
        });
        // Clear this target's prior failure (and the scalar mirror) so a retry
        // starts clean — the map and `error` are updated together, never drift.
        clearDecisionError(key);
        setError(null);
        try {
            const input = create(SubmitFileDecisionInputSchema, {
                agentExecutionId: executionId,
                changeSetId,
                scope,
                fileChangeId,
                action,
                expectedDigest: options?.expectedDigest ?? "",
                reason: options?.reason ?? "",
                acknowledgeUnreviewable: options?.acknowledgeUnreviewable ?? false,
            });
            await stigmer.agentExecution.submitFileDecision(input);
        }
        catch (err) {
            const e = toError(err);
            setDecisionErrors((prev) => new Map(prev).set(key, e));
            setError(e);
            throw err;
        }
        finally {
            setSubmittingKeys((prev) => {
                const next = new Set(prev);
                next.delete(key);
                return next;
            });
        }
    }, [stigmer, clearDecisionError]);
    return useMemo(() => ({
        submitFileDecision,
        submittingDecisionKeys: submittingKeys,
        decisionErrors,
        clearDecisionError,
        error,
        clearError,
    }), [submitFileDecision, submittingKeys, decisionErrors, clearDecisionError, error, clearError]);
}
//# sourceMappingURL=useFileReview.js.map