/**
 * Fast Feature Agent Graph Factory
 *
 * Creates a LangGraph StateGraph with nodes: fast-implement (+ evidence sub-agent) → merge.
 * This is the fast-mode alternative to createFeatureAgentGraph() which has
 * 15+ nodes. The merge node and its CI watch/fix loop are reused via
 * composition from the full graph's createMergeNode(). Evidence collection
 * runs as a sub-agent call within fast-implement, not as a separate node.
 *
 * Uses the same FeatureAgentAnnotation state shape as the full graph,
 * enabling checkpointing, resume, and identical worker lifecycle handling.
 */
import { StateGraph, START, END } from '@langchain/langgraph';
import { FeatureAgentAnnotation } from './state.js';
import { createFastImplementNode } from './nodes/fast-implement.node.js';
import { createMergeNode } from './nodes/merge/merge.node.js';
// Re-export for consumers
export { FeatureAgentAnnotation } from './state.js';
/**
 * Conditional edge: route back to fast-implement on merge rejection,
 * otherwise proceed to END.
 *
 * Mirrors the routeReexecution() pattern from feature-agent-graph.ts.
 */
function routeReexecution(selfNode, nextNode) {
    return (state) => {
        if (state._needsReexecution)
            return selfNode;
        return nextNode;
    };
}
/**
 * Factory function that creates and compiles the fast-mode feature agent graph.
 *
 * The graph defines a minimal workflow:
 *   START → fast-implement (+ evidence sub-agent) → merge → conditional(reexecute or END)
 *
 * Evidence collection runs as a sub-agent call within fast-implement, not as
 * a separate graph node. When merge is rejected, routeReexecution routes back
 * to fast-implement for a fresh implementation pass.
 *
 * @param depsOrExecutor - Graph dependencies or a legacy executor
 * @param checkpointer - Optional checkpoint saver for state persistence
 * @returns A compiled LangGraph ready to be invoked
 */
export function createFastFeatureAgentGraph(depsOrExecutor, checkpointer) {
    // Support legacy signature: createFastFeatureAgentGraph(executor, checkpointer)
    const deps = 'execute' in depsOrExecutor ? { executor: depsOrExecutor } : depsOrExecutor;
    const { executor } = deps;
    const graph = new StateGraph(FeatureAgentAnnotation).addNode('fast-implement', createFastImplementNode(executor));
    graph.addEdge(START, 'fast-implement');
    // Wire merge node when deps are provided
    if (deps.mergeNodeDeps) {
        const mergeNodeDeps = {
            executor,
            ...deps.mergeNodeDeps,
        };
        graph
            .addNode('merge', createMergeNode(mergeNodeDeps))
            .addEdge('fast-implement', 'merge')
            .addConditionalEdges('merge', routeReexecution('fast-implement', END));
    }
    else {
        graph.addEdge('fast-implement', END);
    }
    return graph.compile({ checkpointer });
}
