"use client";
import { useCallback, useRef, useState } from "react";
import { create } from "@bufbuild/protobuf";
import { CancelWorkflowExecutionInputSchema, TerminateWorkflowExecutionInputSchema, PauseWorkflowExecutionInputSchema, ResumeWorkflowExecutionInputSchema, RecoverWorkflowExecutionInputSchema, SubmitWorkflowApprovalInputSchema, SubmitWorkflowTaskApprovalInputSchema, } from "@stigmer/protos/ai/stigmer/agentic/workflowexecution/v1/io_pb";
import { useStigmer } from "../hooks";
import { toError } from "../internal/toError";
/**
 * Behavior hook that encapsulates workflow execution lifecycle actions.
 *
 * Each action calls the corresponding RPC and returns the updated
 * execution, or `null` on failure (with `error` populated).
 *
 * Pass `null` for `executionId` to disable all actions (they become
 * no-ops that return `null`).
 *
 * @example
 * ```tsx
 * const actions = useWorkflowExecutionActions(executionId, {
 *   onSuccess: () => refetchExecution(),
 * });
 *
 * <button onClick={() => actions.cancel("No longer needed")} disabled={actions.isSubmitting}>
 *   Cancel
 * </button>
 * ```
 */
export function useWorkflowExecutionActions(executionId, options) {
    const stigmer = useStigmer();
    const [isSubmitting, setIsSubmitting] = useState(false);
    const [error, setError] = useState(null);
    const executionIdRef = useRef(executionId);
    executionIdRef.current = executionId;
    const stigmerRef = useRef(stigmer);
    stigmerRef.current = stigmer;
    const onSuccessRef = useRef(options?.onSuccess);
    onSuccessRef.current = options?.onSuccess;
    const clearError = useCallback(() => setError(null), []);
    const wrap = useCallback(async (fn, fireOnSuccess = true) => {
        if (!executionIdRef.current)
            return null;
        setIsSubmitting(true);
        setError(null);
        try {
            const result = await fn();
            if (fireOnSuccess)
                onSuccessRef.current?.(result);
            return result;
        }
        catch (err) {
            setError(toError(err));
            return null;
        }
        finally {
            setIsSubmitting(false);
        }
    }, []);
    const cancel = useCallback((reason) => wrap(() => stigmerRef.current.workflowExecution.cancel(create(CancelWorkflowExecutionInputSchema, {
        id: executionIdRef.current,
        reason: reason ?? "",
    }))), [wrap]);
    const terminate = useCallback((reason) => wrap(() => stigmerRef.current.workflowExecution.terminate(create(TerminateWorkflowExecutionInputSchema, {
        id: executionIdRef.current,
        reason: reason ?? "",
    }))), [wrap]);
    const pause = useCallback((reason) => wrap(() => stigmerRef.current.workflowExecution.pause(create(PauseWorkflowExecutionInputSchema, {
        id: executionIdRef.current,
        reason: reason ?? "",
    }))), [wrap]);
    const resume = useCallback(() => wrap(() => stigmerRef.current.workflowExecution.resume(create(ResumeWorkflowExecutionInputSchema, {
        id: executionIdRef.current,
    }))), [wrap]);
    const recover = useCallback((reason) => wrap(() => stigmerRef.current.workflowExecution.recover(create(RecoverWorkflowExecutionInputSchema, {
        id: executionIdRef.current,
        reason: reason ?? "",
    }))), [wrap]);
    const submitApproval = useCallback((toolCallId, action, comment) => wrap(() => stigmerRef.current.workflowExecution.submitApproval(create(SubmitWorkflowApprovalInputSchema, {
        executionId: executionIdRef.current,
        toolCallId,
        action,
        comment: comment ?? "",
    })), false), [wrap]);
    const submitTaskApproval = useCallback((taskName, outcome, formData, comment) => wrap(() => stigmerRef.current.workflowExecution.submitWorkflowTaskApproval(create(SubmitWorkflowTaskApprovalInputSchema, {
        executionId: executionIdRef.current,
        taskName,
        outcome,
        formData: formData,
        reviewer: "",
        comment: comment ?? "",
    })), false), [wrap]);
    return {
        cancel,
        terminate,
        pause,
        resume,
        recover,
        submitApproval,
        submitTaskApproval,
        isSubmitting,
        error,
        clearError,
    };
}
//# sourceMappingURL=useWorkflowExecutionActions.js.map