"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.startStepGroupExecutor = void 0;
const ExecuteStepGroupStep_1 = __importDefault(require("../../sharedUtils/mablscript/extended/ExecuteStepGroupStep"));
const stepIndexUtils_1 = require("../../sharedUtils/stepIndexUtils");
exports.startStepGroupExecutor = {
    needsFind: () => false,
    getStepType: () => 'start_step_group',
    execute: async (payload, stateProvider, eventHandler, logger) => {
        const toolParams = payload.toolCall
            .toolParams;
        const stepGroupName = toolParams?.step_group_name;
        if (!stepGroupName) {
            logger.error('StartStepGroupExecutor requires step_group_name argument');
            return {
                success: false,
                errorCause: 'IllegalArguments',
                errorMessage: 'step_group_name is required',
            };
        }
        if (eventHandler.isInsideStepGroup()) {
            eventHandler.moveCursorOutOfGroup();
        }
        const stepGroup = new ExecuteStepGroupStep_1.default([], stepGroupName);
        await eventHandler.dispatchSteps([stepGroup], {
            skipActiveGroupInsertion: true,
        });
        if (!eventHandler.isInsideStepGroup()) {
            eventHandler.moveCursorIntoGroup();
        }
        const newCursor = stateProvider.getNextStepIndex();
        logger.info(`Started step group: ${stepGroupName}`);
        const cursorInfo = newCursor
            ? ` Cursor is now at ${(0, stepIndexUtils_1.pathToStepIndex)(newCursor)} (inside the new group).`
            : '';
        return {
            success: true,
            message: `Started step group "${stepGroupName}".${cursorInfo}`,
        };
    },
};
