"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.runStepCommand = void 0;
exports.runStep = runStep;
const debugFailWrapper_1 = require("../util/debugFailWrapper");
const constants_1 = require("../../../constants");
const debugCommands_1 = require("../../debugCommands");
const DebugStepExecutor_1 = require("@mablhq-internal/runtime/debug/DebugStepExecutor");
const DebugSession_1 = require("@mablhq-internal/runtime/debug/DebugSession");
const debugCursorBridge_1 = require("@mablhq-internal/runtime/debug/debugCursorBridge");
const resolveStepReference_1 = require("@mablhq-internal/runtime/debug/resolveStepReference");
exports.runStepCommand = (0, debugCommands_1.defineLeaf)({
    command: `run-step <${constants_1.CommandArgSessionId}> [${constants_1.CommandArgStepId}]`,
    describe: 'Execute a step in the debug session. Pass <step-ref> to run a specific step; omit it to run the step at the canonical-tree cursor (the same position list-steps shows as isCursor and set-current-step writes to). A <step-ref> can be the canonical step id, the 1-based flat position from list-steps (e.g. "14"), dot-notation through the step tree (e.g. "3.2"), or the synthetic "<flow-id>:<idx>" id list-steps falls back to when the underlying flow has no json_steps ids.',
    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',
            describe: 'Step reference: canonical step id, 1-based flat position (e.g. "14"), dot-notation (e.g. "3.2"), or synthetic "<flow-id>:<idx>". Omit to run the step at the cursor.',
        });
    },
    handler: (0, debugFailWrapper_1.debugFailWrapper)(runStep),
});
async function runStep(parsed) {
    const sessionId = parsed[constants_1.CommandArgSessionId];
    const explicitStepRef = parsed[constants_1.CommandArgStepId];
    let stepId;
    if (explicitStepRef !== undefined) {
        const session = (0, DebugSession_1.readSession)(sessionId);
        const resolved = (0, resolveStepReference_1.resolveStepReference)(session.snapshot, explicitStepRef);
        if (!resolved.success) {
            process.stderr.write(resolved.errorMessage + '\n');
            process.exit(2);
            return;
        }
        stepId = resolved.stepId;
    }
    else {
        stepId = await resolveCursorStepId(sessionId);
    }
    if (stepId === undefined) {
        process.stderr.write('Cursor is parked past the last step; nothing to run. Pass a <step-ref> explicitly or move the cursor with set-current-step.\n');
        process.exit(2);
        return;
    }
    const result = await (0, DebugStepExecutor_1.executeDebugStep)(sessionId, stepId);
    process.stdout.write(JSON.stringify(result) + '\n');
    if (result.status === 'failed') {
        process.exit(1);
    }
}
async function resolveCursorStepId(sessionId) {
    const session = (0, DebugSession_1.readSession)(sessionId);
    return (0, debugCursorBridge_1.getCurrentCursorStepId)(sessionId, session.snapshot, session.currentStepIndex);
}
