"use client";
import { useEffect, useRef } from "react";
import { ExecutionPhase } from "@stigmer/protos/ai/stigmer/agentic/workflowexecution/v1/enum_pb";
import { isNotFound } from "@stigmer/sdk";
import { useStigmer } from "../hooks";
import { useFetch } from "../internal/useFetch";
import { useRunnerAdapter } from "../runner-adapter";
import { useExecutionTarget } from "../execution-target-context";
const TERMINAL_EXECUTION_PHASES = new Set([
    ExecutionPhase.EXECUTION_COMPLETED,
    ExecutionPhase.EXECUTION_FAILED,
    ExecutionPhase.EXECUTION_CANCELLED,
    ExecutionPhase.EXECUTION_TERMINATED,
]);
/**
 * Data hook that fetches a single WorkflowExecution by ID.
 *
 * Pass `null` for `executionId` to skip fetching (stable no-op).
 * When the ID changes, the previous request is discarded and a
 * fresh fetch begins.
 *
 * If the API returns NOT_FOUND, `execution` is set to `null`
 * without raising an error.
 *
 * @example
 * ```tsx
 * function ExecutionHeader({ id }: { id: string }) {
 *   const { execution, isLoading, error } = useWorkflowExecution(id);
 *   if (isLoading) return <Skeleton />;
 *   if (error) return <ErrorMessage error={error} />;
 *   if (!execution) return <NotFound />;
 *   return <h2>{execution.metadata?.name}</h2>;
 * }
 * ```
 */
export function useWorkflowExecution(executionId) {
    const stigmer = useStigmer();
    const adapter = useRunnerAdapter();
    const contextTarget = useExecutionTarget();
    const terminatedRef = useRef(null);
    const fetchFn = executionId
        ? async () => {
            try {
                return await stigmer.workflowExecution.get(executionId);
            }
            catch (err) {
                if (isNotFound(err))
                    return null;
                throw err;
            }
        }
        : null;
    const { data: execution, isLoading, isRefetching, error, refetch } = useFetch(fetchFn, [executionId, stigmer], null, { cacheKey: executionId ? `workflow-execution:${executionId}` : undefined });
    useEffect(() => {
        const phase = execution?.status?.phase;
        const fetchedId = execution?.metadata?.id;
        if (adapter &&
            contextTarget === "local" &&
            executionId &&
            phase != null &&
            TERMINAL_EXECUTION_PHASES.has(phase) &&
            fetchedId === executionId &&
            terminatedRef.current !== executionId) {
            terminatedRef.current = executionId;
            adapter.onWorkflowExecutionTerminated(executionId).catch(() => { });
        }
    }, [execution?.status?.phase, execution?.metadata?.id, executionId, adapter, contextTarget]);
    useEffect(() => {
        terminatedRef.current = null;
    }, [executionId]);
    return { execution, isLoading, isRefetching, error, refetch };
}
//# sourceMappingURL=useWorkflowExecution.js.map