"use client";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { create } from "@bufbuild/protobuf";
import { StigmerError } from "@stigmer/sdk";
import { GetWorkflowVersionInputSchema, } from "@stigmer/protos/ai/stigmer/agentic/workflow/v1/version_pb";
import { useStigmer } from "../hooks";
import { toError } from "../internal/toError";
const CODE_UNIMPLEMENTED = 12;
// ---------------------------------------------------------------------------
// Hook implementation
// ---------------------------------------------------------------------------
/**
 * Data hook that fetches a single workflow version entry by ID and hash.
 *
 * Calls `stigmer.workflow.getVersion()` to retrieve the full version
 * details for a specific content hash. Useful for inspecting a
 * particular historical version outside the timeline listing.
 *
 * **Graceful degradation**: If the backend returns UNIMPLEMENTED, the
 * hook resolves to `null` without raising an error.
 *
 * Pass `null` for either `workflowId` or `versionHash` to skip
 * fetching (stable no-op).
 *
 * @example
 * ```tsx
 * const { version, isLoading } = useWorkflowVersion(workflowId, selectedHash);
 *
 * if (version) {
 *   return <pre>{version.validatedYaml}</pre>;
 * }
 * ```
 */
export function useWorkflowVersion(workflowId, versionHash) {
    const stigmer = useStigmer();
    const [version, setVersion] = useState(null);
    const [isLoading, setIsLoading] = useState(false);
    const [error, setError] = useState(null);
    const fetchIdRef = useRef(0);
    const doFetch = useCallback(async () => {
        if (!workflowId || !versionHash) {
            setVersion(null);
            setError(null);
            return;
        }
        const fetchId = ++fetchIdRef.current;
        setIsLoading(true);
        setError(null);
        try {
            const result = await stigmer.workflow.getVersion(create(GetWorkflowVersionInputSchema, { workflowId, versionHash }));
            if (fetchIdRef.current !== fetchId)
                return;
            setVersion(result);
        }
        catch (err) {
            if (fetchIdRef.current !== fetchId)
                return;
            if (isUnimplemented(err)) {
                setVersion(null);
            }
            else {
                setError(toError(err));
                setVersion(null);
            }
        }
        finally {
            if (fetchIdRef.current === fetchId) {
                setIsLoading(false);
            }
        }
    }, [workflowId, versionHash, stigmer]);
    useEffect(() => {
        doFetch();
    }, [doFetch]);
    const refetch = useCallback(() => {
        doFetch();
    }, [doFetch]);
    return useMemo(() => ({ version, isLoading, error, refetch }), [version, isLoading, error, refetch]);
}
// ---------------------------------------------------------------------------
// Error detection
// ---------------------------------------------------------------------------
function isUnimplemented(err) {
    return err instanceof StigmerError && err.connectCode === CODE_UNIMPLEMENTED;
}
//# sourceMappingURL=useWorkflowVersion.js.map