"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.runStepSequence = runStepSequence;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const DebugStepExecutor_1 = require("@mablhq-internal/runtime/debug/DebugStepExecutor");
const DebugSession_1 = require("@mablhq-internal/runtime/debug/DebugSession");
const DESCRIPTION_PREVIEW_LIMIT = 60;
const PROGRESS_FILE_NAME = 'run-progress.jsonl';
function progressFilePath(sessionId) {
    return path.join((0, DebugSession_1.sessionDir)(sessionId), PROGRESS_FILE_NAME);
}
async function runStepSequence(sessionId, steps) {
    const progressFile = progressFilePath(sessionId);
    fs.writeFileSync(progressFile, '');
    let anyFailed = false;
    for (let i = 0; i < steps.length; i++) {
        const step = steps[i];
        const result = await (0, DebugStepExecutor_1.executeDebugStep)(sessionId, step.stepId);
        process.stdout.write(formatProgressLine(i + 1, steps.length, step, result));
        appendProgressEvent(progressFile, i + 1, steps.length, step, result);
        if (result.status !== 'passed') {
            anyFailed = true;
            break;
        }
    }
    if (anyFailed) {
        process.exitCode = 1;
    }
}
function formatProgressLine(position, total, step, result) {
    const status = result.status === 'passed' ? 'PASS' : 'FAIL';
    const description = truncate(step.description, DESCRIPTION_PREVIEW_LIMIT);
    const errorSuffix = result.status === 'failed' && result.error ? `  ${result.error}` : '';
    return `[${position}/${total}] ${status}  ${step.action}  "${description}"  ${result.durationMs}ms${errorSuffix}\n`;
}
function appendProgressEvent(progressFile, position, total, step, result) {
    const event = {
        step: position,
        total,
        stepId: step.stepId,
        action: step.action,
        description: step.description,
        status: result.status,
        durationMs: result.durationMs,
    };
    if (result.error !== undefined) {
        event.error = result.error;
    }
    fs.appendFileSync(progressFile, JSON.stringify(event) + '\n');
}
const ELLIPSIS = '...';
function truncate(value, limit) {
    if (value.length <= limit) {
        return value;
    }
    return `${value.slice(0, limit - ELLIPSIS.length)}${ELLIPSIS}`;
}
