"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.testAssertStepExecutor = void 0;
const stepIndexUtils_1 = require("../../sharedUtils/stepIndexUtils");
const mablscript_step_types_1 = require("../../sharedUtils/mablscript-step-types");
exports.testAssertStepExecutor = {
    needsFind: () => false,
    getStepType: () => 'test_assert_step',
    execute: async (payload, stateProvider, eventHandler, logger) => {
        const toolParams = payload.toolCall.toolParams;
        const stepIndex = toolParams?.step_index;
        if (!stepIndex) {
            return {
                success: false,
                errorCause: 'IllegalArguments',
                errorMessage: 'step_index is required (1-based dot-notation, e.g. "1", "2.3")',
            };
        }
        let stepPath;
        try {
            stepPath = (0, stepIndexUtils_1.parseStepIndex)(stepIndex);
        }
        catch (error) {
            return {
                success: false,
                errorCause: 'IllegalArguments',
                errorMessage: error instanceof Error ? error.message : 'Invalid step index format',
            };
        }
        const allSteps = stateProvider.getSteps();
        const getNestedSteps = (0, stepIndexUtils_1.createNestedStepResolver)(stateProvider.getImportedFlowSteps.bind(stateProvider));
        const step = (0, stepIndexUtils_1.getStepAtPath)(allSteps, stepPath, getNestedSteps);
        if (!step) {
            return {
                success: false,
                errorCause: 'IllegalArguments',
                errorMessage: `No step found at index ${stepIndex}`,
            };
        }
        const stepType = step.stepType;
        if (!(0, mablscript_step_types_1.stepIsStepWithAssertion)(step) &&
            stepType !== mablscript_step_types_1.StepType.ACCESSIBILITY_CHECK) {
            return {
                success: false,
                errorCause: 'IllegalArguments',
                errorMessage: `Step at index ${stepIndex} is a ${stepType} step, not an assertion step`,
            };
        }
        logger.info(`Testing assert step at index ${stepIndex}`);
        try {
            const result = await eventHandler.runSingleStep(step);
            if (result.success === false) {
                return {
                    success: false,
                    errorMessage: result.message ?? `Assert step at ${stepIndex} failed`,
                };
            }
            return {
                success: true,
                message: `Assert step at ${stepIndex} passed`,
            };
        }
        catch (error) {
            return {
                success: false,
                errorMessage: error instanceof Error
                    ? error.message
                    : `Assert step at ${stepIndex} threw an unexpected error`,
            };
        }
    },
};
