"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.runToStepCommand = void 0;
exports.runToStep = runToStep;
const debugFailWrapper_1 = require("../util/debugFailWrapper");
const constants_1 = require("../../../constants");
const debugCommands_1 = require("../../debugCommands");
const DebugSession_1 = require("@mablhq-internal/runtime/debug/DebugSession");
const expandSteps_1 = require("@mablhq-internal/runtime/debug/expandSteps");
const debugCursorBridge_1 = require("@mablhq-internal/runtime/debug/debugCursorBridge");
const resolveStepReference_1 = require("@mablhq-internal/runtime/debug/resolveStepReference");
const runStepSequence_1 = require("./util/runStepSequence");
exports.runToStepCommand = (0, debugCommands_1.defineLeaf)({
    command: `run-to-step <${constants_1.CommandArgSessionId}> <${constants_1.CommandArgStepId}>`,
    describe: 'Run all steps from current position to the target step (inclusive), stopping on first failure. <step-ref> accepts the canonical step id, the 1-based flat position from list-steps (e.g. "14"), dot-notation (e.g. "3.2"), or the synthetic "<flow-id>:<idx>" fallback.',
    builder: (yargs) => {
        yargs
            .positional(constants_1.CommandArgSessionId, {
            type: 'string',
            demandOption: true,
            describe: 'Debug session id (returned by `session start`).',
        })
            .positional(constants_1.CommandArgStepId, {
            type: 'string',
            demandOption: true,
            describe: 'Target step reference: canonical step id, 1-based flat position (e.g. "14"), dot-notation (e.g. "3.2"), or synthetic "<flow-id>:<idx>".',
        });
    },
    handler: (0, debugFailWrapper_1.debugFailWrapper)(runToStep),
});
async function runToStep(parsed) {
    const sessionId = parsed[constants_1.CommandArgSessionId];
    const targetStepRef = parsed[constants_1.CommandArgStepId];
    const session = (0, DebugSession_1.readSession)(sessionId);
    const expanded = (0, expandSteps_1.expandSessionSteps)(session.snapshot);
    const resolved = (0, resolveStepReference_1.resolveStepReference)(session.snapshot, targetStepRef);
    if (!resolved.success) {
        process.stderr.write(resolved.errorMessage + '\n');
        process.exit(2);
        return;
    }
    const targetIndex = resolved.flatIndex;
    const startIndex = await (0, debugCursorBridge_1.getCurrentCursorFlatIndex)(sessionId, session.snapshot, session.currentStepIndex);
    if (targetIndex < startIndex) {
        process.stderr.write(`Target step ${targetStepRef} (index ${targetIndex}) is before current position (${startIndex})\n`);
        process.exit(2);
        return;
    }
    const steps = [];
    for (let i = startIndex; i <= targetIndex;) {
        const entry = expanded[i];
        steps.push({
            stepId: (0, expandSteps_1.addressableStepId)(entry, i),
            action: entry.step.getStepName(),
            description: entry.step.getResolvedDescription(),
        });
        if (entry.subtreeEndIndex >= targetIndex) {
            break;
        }
        i = entry.subtreeEndIndex + 1;
    }
    await (0, runStepSequence_1.runStepSequence)(sessionId, steps);
}
