"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getStepJsonExecutor = void 0;
const stepIndexUtils_1 = require("../../sharedUtils/stepIndexUtils");
const stepSerialization_1 = require("../../sharedUtils/mablscript/stepSerialization");
const variableCanonical_1 = require("@mablhq/test-model/variableCanonical");
exports.getStepJsonExecutor = {
    needsFind: () => false,
    getStepType: () => 'get-step-json',
    execute: (payload, stateProvider, _eventHandler, logger) => {
        const toolParams = payload.toolCall.toolParams;
        const stepIds = toolParams.step_ids ?? [];
        const allSteps = stateProvider.getSteps();
        const variables = stateProvider.getVariables();
        const aliased = (0, variableCanonical_1.aliasVariables)(variables);
        const steps = stepIds.map((stepId) => {
            const resolution = (0, stepIndexUtils_1.resolveStepPathFromIdOrIndex)(allSteps, stepIndexUtils_1.getStepGroupNestedSteps, stepId, undefined, { idField: 'step_id', indexField: 'step_id' });
            if ('error' in resolution) {
                return { step_id: stepId, found: false };
            }
            const step = (0, stepIndexUtils_1.getStepAtPath)(allSteps, resolution.path, stepIndexUtils_1.getStepGroupNestedSteps);
            if (!step) {
                return { step_id: stepId, found: false };
            }
            const json = (0, stepSerialization_1.parseStepToV2Format)(step, false, variables, aliased);
            return { step_id: stepId, found: true, json: JSON.stringify(json) };
        });
        logger.info(`get_step_json: ${steps.filter((s) => s.found).length}/${stepIds.length} resolved`);
        return Promise.resolve({ success: true, successDetail: { steps } });
    },
};
