"use client";
import { useMemo, useState } from "react";
import { yamlToGraph, toReactFlowElements } from "./workflow-graph-conversions.js";
import { applyDagreLayout } from "./layout/index.js";
import { computeGraphDiff, buildDiffGraph } from "./diff/index.js";
const EMPTY_NODES = [];
const EMPTY_EDGES = [];
/**
 * Behavior hook that builds a complete diff visualization from two YAML strings.
 *
 * Pipeline (all in useMemo for referential stability — DD-010):
 * 1. Parse beforeYaml → before graph model (empty string → empty model)
 * 2. Parse afterYaml → after graph model
 * 3. Compute diff between before and after
 * 4. Build merged graph containing all nodes/edges
 * 5. Apply dagre layout to merged graph
 * 6. Convert to React Flow elements
 * 7. Merge diff status into node/edge data
 *
 * @since T14 (AI-Assisted Workflow Creation)
 */
export function useWorkflowDiffGraph(options) {
    const { beforeYaml, afterYaml } = options;
    const [selectedTaskName, setSelectedTaskName] = useState(null);
    const result = useMemo(() => {
        // Both empty → no diff
        if (!afterYaml.trim()) {
            return { nodes: EMPTY_NODES, edges: EMPTY_EDGES, diff: null, error: null };
        }
        try {
            const afterGraph = yamlToGraph(afterYaml);
            // Empty before → generate scenario: all nodes are "added"
            if (!beforeYaml.trim()) {
                const emptyBefore = {
                    document: afterGraph.document,
                    nodes: [],
                    edges: [],
                };
                const diff = computeGraphDiff(emptyBefore, afterGraph);
                const merged = buildDiffGraph(emptyBefore, afterGraph, diff);
                const laidOut = applyDagreLayout(merged);
                const elements = toReactFlowElements(laidOut);
                return {
                    ...mergeOverlays(elements, diff),
                    diff,
                    error: null,
                };
            }
            const beforeGraph = yamlToGraph(beforeYaml);
            const diff = computeGraphDiff(beforeGraph, afterGraph);
            const merged = buildDiffGraph(beforeGraph, afterGraph, diff);
            const laidOut = applyDagreLayout(merged);
            const elements = toReactFlowElements(laidOut);
            return {
                ...mergeOverlays(elements, diff),
                diff,
                error: null,
            };
        }
        catch (err) {
            const message = err instanceof Error ? err.message : "Failed to compute diff";
            return { nodes: EMPTY_NODES, edges: EMPTY_EDGES, diff: null, error: message };
        }
    }, [beforeYaml, afterYaml]);
    return {
        nodes: result.nodes,
        edges: result.edges,
        diff: result.diff,
        error: result.error,
        selectedTaskName,
        setSelectedTaskName,
    };
}
// ---------------------------------------------------------------------------
// Overlay merging
// ---------------------------------------------------------------------------
function mergeOverlays(elements, diff) {
    const nodes = elements.nodes.map((node) => {
        const nodeData = node.data;
        const entry = diff.nodes.get(node.id);
        if (!entry)
            return node;
        return {
            ...node,
            data: {
                ...nodeData,
                diffState: {
                    status: entry.status,
                    changedFields: entry.changedFields,
                },
            },
            draggable: false,
            connectable: false,
            deletable: false,
        };
    });
    const edges = elements.edges.map((edge) => {
        const edgeData = (edge.data ?? {});
        const edgeKey = edge.data?.sourceHandle
            ? `${edge.source}|${edge.target}|${edge.sourceHandle}`
            : `${edge.source}|${edge.target}`;
        const entry = diff.edges.get(edgeKey);
        if (!entry)
            return edge;
        return {
            ...edge,
            data: {
                ...edgeData,
                diffState: entry.status,
            },
        };
    });
    return { nodes, edges };
}
//# sourceMappingURL=useWorkflowDiffGraph.js.map