"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.runAllCommand = void 0;
exports.runAll = runAll;
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 runStepSequence_1 = require("./util/runStepSequence");
exports.runAllCommand = (0, debugCommands_1.defineLeaf)({
    command: `run-all <${constants_1.CommandArgSessionId}>`,
    describe: 'Run all remaining steps from current position to end, stopping on first failure',
    builder: (yargs) => {
        yargs.positional(constants_1.CommandArgSessionId, {
            type: 'string',
            demandOption: true,
            describe: 'Debug session id (returned by `session start`).',
        });
    },
    handler: (0, debugFailWrapper_1.debugFailWrapper)(runAll),
});
async function runAll(parsed) {
    const sessionId = parsed[constants_1.CommandArgSessionId];
    const session = (0, DebugSession_1.readSession)(sessionId);
    const startIndex = await (0, debugCursorBridge_1.getCurrentCursorFlatIndex)(sessionId, session.snapshot, session.currentStepIndex);
    const expanded = (0, expandSteps_1.expandSessionSteps)(session.snapshot);
    const steps = [];
    for (let i = startIndex; i < expanded.length;) {
        const entry = expanded[i];
        steps.push({
            stepId: (0, expandSteps_1.addressableStepId)(entry, i),
            action: entry.step.getStepName(),
            description: entry.step.getResolvedDescription(),
        });
        i = entry.subtreeEndIndex + 1;
    }
    await (0, runStepSequence_1.runStepSequence)(sessionId, steps);
}
