"use strict";
/**
 * DAG construction helpers for the build pipeline stage.
 *
 * Encapsulates the mutable state of DAG assembly (activities, transitions,
 * manifest) behind semantic operations so the main build() function reads
 * as a high-level pipeline.
 *
 * Step-appender functions (appendIterationStep, appendNormalStep,
 * appendSignalStep) live in dag-assembly.ts.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.appendSignalStep = exports.appendNormalStep = exports.appendIterationStep = void 0;
exports.initializeDag = initializeDag;
exports.serializeToYaml = serializeToYaml;
// eslint-disable-next-line @typescript-eslint/no-var-requires
const yaml = require('js-yaml');
const defaults_1 = require("../../../../modules/defaults");
// Re-export step appenders so existing import sites continue to work
var dag_assembly_1 = require("./dag-assembly");
Object.defineProperty(exports, "appendIterationStep", { enumerable: true, get: function () { return dag_assembly_1.appendIterationStep; } });
Object.defineProperty(exports, "appendNormalStep", { enumerable: true, get: function () { return dag_assembly_1.appendNormalStep; } });
Object.defineProperty(exports, "appendSignalStep", { enumerable: true, get: function () { return dag_assembly_1.appendSignalStep; } });
// ── DAG lifecycle ────────────────────────────────────────────────────────────
/** Create the DAG with its trigger activity. */
function initializeDag(prefix, graphTopic, inputSchema) {
    const triggerId = `${prefix}_t1`;
    const activities = {};
    const transitions = {};
    activities[triggerId] = {
        title: 'Trigger',
        type: 'trigger',
        output: { schema: { type: 'object' } },
    };
    const manifest = [{
            activity_id: triggerId,
            title: 'Trigger',
            type: 'trigger',
            tool_source: 'trigger',
            topic: graphTopic,
            input_mappings: {},
            output_fields: Object.keys(inputSchema.properties || {}),
        }];
    return {
        activities,
        transitions,
        hooks: {},
        manifest,
        stepIndexToActivityId: new Map(),
        prevActivityId: triggerId,
        prevResult: null,
        lastPivotId: null,
        triggerId,
    };
}
// ── Serialization ────────────────────────────────────────────────────────────
/** Assemble the DAG into a HotMesh YAML document. */
function serializeToYaml(appId, graphTopic, inputSchema, outputSchema, dag) {
    const graph = {
        subscribes: graphTopic,
        expire: defaults_1.WORKFLOW_EXPIRE_SECS,
        input: { schema: inputSchema },
        output: { schema: outputSchema },
        activities: dag.activities,
        transitions: dag.transitions,
    };
    // Include hooks section when signal steps are present
    if (Object.keys(dag.hooks).length > 0) {
        graph.hooks = dag.hooks;
    }
    const graphDef = {
        app: {
            id: appId,
            version: '1',
            graphs: [graph],
        },
    };
    return yaml.dump(graphDef, {
        lineWidth: defaults_1.YAML_LINE_WIDTH,
        noRefs: true,
        sortKeys: false,
    });
}
