"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.moveCursorExecutor = void 0;
const stepIndexUtils_1 = require("../../sharedUtils/stepIndexUtils");
const cursorOperations_1 = require("../cursorOperations");
exports.moveCursorExecutor = {
    needsFind: () => false,
    getStepType: () => 'move_cursor',
    execute: (payload, stateProvider, eventHandler, logger) => {
        const toolParams = payload.toolCall
            .toolParams;
        const targetPosition = toolParams?.target_position;
        if (!targetPosition) {
            return Promise.resolve({
                success: false,
                errorCause: 'IllegalArguments',
                errorMessage: 'target_position is required (1-based dot-notation, e.g. "2", "2.3")',
            });
        }
        let path;
        try {
            path = (0, stepIndexUtils_1.parseStepIndex)(targetPosition);
        }
        catch (error) {
            return Promise.resolve({
                success: false,
                errorCause: 'IllegalArguments',
                errorMessage: error instanceof Error
                    ? error.message
                    : 'Invalid target_position format',
            });
        }
        const allSteps = stateProvider.getSteps();
        const getNestedSteps = (0, stepIndexUtils_1.createNestedStepResolver)(stateProvider.getImportedFlowSteps.bind(stateProvider));
        const validation = (0, cursorOperations_1.validateMoveCursorTarget)(allSteps, getNestedSteps, path);
        if (!validation.success) {
            const errorCause = validation.errorMessage?.startsWith('Invalid position')
                ? 'IllegalArguments'
                : undefined;
            return Promise.resolve({
                success: false,
                errorCause,
                errorMessage: validation.errorMessage,
            });
        }
        eventHandler.setNextStepIndex(path);
        logger.info(`Moved cursor to position ${targetPosition} (path: [${path}])`);
        return Promise.resolve({ success: true });
    },
};
