/**
 * Pure params→fragment expanders. A new template is purely additive here — no
 * generalization of this signature until a 2nd/3rd template earns it (the n=1
 * discipline: the abstraction stays shaped by concrete instances).
 */
const FLOW_TEMPLATES = {
    // loopback_gate: being in `state`, a `trigger` (e.g. an audit-fail) loops back to
    // `back_to` to redo the region. A loop-back connects two EXISTING spine states, so
    // it contributes only the edge — both endpoints MUST already be declared in
    // fsm.yaml. validateFsm (run on the merged machine) then catches a typo'd endpoint.
    loopback_gate: (params) => {
        const { state, trigger, back_to } = params;
        if (typeof state !== 'string' || typeof trigger !== 'string' || typeof back_to !== 'string') {
            return {
                ok: false,
                error: 'loopback_gate requires string params { state, trigger, back_to }',
            };
        }
        return {
            ok: true,
            expansion: { states: [], transitions: [{ from: state, on: trigger, to: back_to }] },
        };
    },
};
/**
 * Compile a pack's `flows` into one merged `{states, transitions}` fragment.
 * Empty `flows` → an empty (no-op) expansion. An unknown template or invalid
 * params is collected (prefixed with the pack name) and surfaced as
 * `{ok:false, errors}` — never silently dropped (mirrors `compileGuards`).
 */
export function compileFlows(packName, flows) {
    const errors = [];
    const states = [];
    const transitions = [];
    for (const flow of flows) {
        const expander = FLOW_TEMPLATES[flow.template];
        if (expander === undefined) {
            errors.push(`pack ${packName}: unknown flow template "${flow.template}" (known: ${Object.keys(FLOW_TEMPLATES).join(', ')})`);
            continue;
        }
        const result = expander(flow.params);
        if (!result.ok) {
            errors.push(`pack ${packName}: ${flow.template}: ${result.error}`);
            continue;
        }
        for (const s of result.expansion.states)
            if (!states.includes(s))
                states.push(s);
        transitions.push(...result.expansion.transitions);
    }
    if (errors.length > 0)
        return { ok: false, errors };
    return { ok: true, expansion: { states, transitions } };
}
//# sourceMappingURL=flows_compiler.js.map