"use client";
import { useCallback, useMemo } from "react";
import { create } from "@bufbuild/protobuf";
import { timestampDate } from "@bufbuild/protobuf/wkt";
import { StigmerError } from "@stigmer/sdk";
import { ListWorkflowVersionsInputSchema, } from "@stigmer/protos/ai/stigmer/agentic/workflow/v1/version_pb";
import { useStigmer } from "../hooks.js";
import { useFetch } from "../internal/useFetch.js";
const CODE_UNIMPLEMENTED = 12;
// ---------------------------------------------------------------------------
// Hook implementation
// ---------------------------------------------------------------------------
/**
 * Data hook that fetches version history for a workflow.
 *
 * Calls the `listVersions` RPC via the SDK client and maps the response
 * to generic {@link VersionEntry} objects for the `VersionTimeline`
 * component.
 *
 * **Graceful degradation**: If the backend returns UNIMPLEMENTED (the
 * RPC handler hasn't been deployed yet), the hook treats it as an empty
 * response — `versions` is `[]` and `isEmpty` is `true`. No error is
 * surfaced to the consumer.
 *
 * Pass `null` for `org` or `slug` to skip fetching (stable no-op).
 *
 * @example
 * ```tsx
 * const { versions, isEmpty, isLoading } = useWorkflowVersions("acme", "deploy-pipeline");
 *
 * if (!isEmpty) {
 *   return <VersionTimeline entries={versions} />;
 * }
 * ```
 */
export function useWorkflowVersions(org, slug) {
    const stigmer = useStigmer();
    const { data: rawVersions, isLoading, error, refetch, } = useFetch(org && slug
        ? async () => {
            try {
                const request = create(ListWorkflowVersionsInputSchema, {
                    org,
                    slug,
                });
                const response = await stigmer.workflow.listVersions(request);
                return response.versions;
            }
            catch (err) {
                if (isUnimplemented(err))
                    return [];
                throw err;
            }
        }
        : null, [org, slug, stigmer], []);
    const versions = useMemo(() => rawVersions.map(mapProtoToVersionEntry), [rawVersions]);
    const validatedYamlMap = useMemo(() => {
        const map = new Map();
        for (const entry of rawVersions) {
            if (entry.versionHash && entry.validatedYaml) {
                map.set(entry.versionHash, entry.validatedYaml);
            }
        }
        return map;
    }, [rawVersions]);
    const getValidatedYaml = useCallback((versionHash) => validatedYamlMap.get(versionHash) ?? null, [validatedYamlMap]);
    const isEmpty = versions.length === 0;
    return useMemo(() => ({ versions, isEmpty, isLoading, error, refetch, getValidatedYaml }), [versions, isEmpty, isLoading, error, refetch, getValidatedYaml]);
}
// ---------------------------------------------------------------------------
// Proto → generic mapping
// ---------------------------------------------------------------------------
function mapProtoToVersionEntry(proto) {
    return {
        id: proto.versionHash,
        timestamp: proto.appliedAt ? timestampDate(proto.appliedAt) : new Date(0),
        actor: proto.appliedBy
            ? {
                id: proto.appliedBy.id,
                avatar: proto.appliedBy.avatar || undefined,
                displayName: undefined,
            }
            : undefined,
        label: proto.versionHash.slice(0, 12),
        sublabel: proto.message || undefined,
        isCurrent: proto.isCurrent,
        tag: proto.tag || undefined,
        gitProvenance: proto.gitProvenance
            ? {
                remoteUrl: proto.gitProvenance.remoteUrl,
                ref: proto.gitProvenance.ref,
                commit: proto.gitProvenance.commit,
            }
            : undefined,
    };
}
// ---------------------------------------------------------------------------
// Error detection
// ---------------------------------------------------------------------------
function isUnimplemented(err) {
    return err instanceof StigmerError && err.connectCode === CODE_UNIMPLEMENTED;
}
//# sourceMappingURL=useWorkflowVersions.js.map