"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteStepsFlexExecutor = void 0;
const stepIndexUtils_1 = require("../../sharedUtils/stepIndexUtils");
exports.deleteStepsFlexExecutor = {
    needsFind: () => false,
    getStepType: () => 'execute_delete_steps',
    execute: (payload, stateProvider, eventHandler, logger) => {
        const toolParams = payload.toolCall
            .toolParams;
        const targetPosition = toolParams?.target_position;
        const endPosition = toolParams?.end_position;
        const stepId = toolParams?.step_id;
        const endStepId = toolParams?.end_step_id;
        if (!targetPosition && stepId === undefined) {
            return Promise.resolve({
                success: false,
                errorMessage: 'Either target_position or step_id is required',
            });
        }
        if (stepId !== undefined && targetPosition !== undefined) {
            logger.debug(`Both step_id and target_position provided; using step_id "${stepId}" (id-based addressing wins).`);
        }
        if (endStepId !== undefined && endPosition !== undefined) {
            logger.debug(`Both end_step_id and end_position provided; using end_step_id "${endStepId}" (id-based addressing wins).`);
        }
        const allSteps = stateProvider.getSteps();
        const startResolution = (0, stepIndexUtils_1.resolveStepPathFromIdOrIndex)(allSteps, stepIndexUtils_1.getStepGroupNestedSteps, stepId, targetPosition, { idField: 'step_id', indexField: 'target_position' });
        if ('error' in startResolution) {
            return Promise.resolve({
                success: false,
                errorMessage: startResolution.error,
            });
        }
        const startPath = startResolution.path;
        const startLabel = (0, stepIndexUtils_1.pathToStepIndex)(startPath);
        if ((0, stepIndexUtils_1.isCursorInsideFlow)(allSteps, startPath)) {
            return Promise.resolve({
                success: false,
                errorMessage: 'Cannot delete individual steps inside a reusable flow. To remove the flow, delete the flow step itself.',
            });
        }
        if (endPosition !== undefined || endStepId !== undefined) {
            const endResolution = (0, stepIndexUtils_1.resolveStepPathFromIdOrIndex)(allSteps, stepIndexUtils_1.getStepGroupNestedSteps, endStepId, endPosition, { idField: 'end_step_id', indexField: 'end_position' });
            if ('error' in endResolution) {
                return Promise.resolve({
                    success: false,
                    errorMessage: endResolution.error,
                });
            }
            const endPath = endResolution.path;
            const endLabel = (0, stepIndexUtils_1.pathToStepIndex)(endPath);
            const error = eventHandler.deleteStepsInRange(startPath, endPath);
            if (error) {
                return Promise.resolve({
                    success: false,
                    errorMessage: error,
                });
            }
            const deleteCount = endPath[endPath.length - 1] - startPath[startPath.length - 1] + 1;
            const newCursor = stateProvider.getNextStepIndex();
            return Promise.resolve({
                success: true,
                message: `Deleted ${deleteCount} step(s) from position ${startLabel} to ${endLabel}. Cursor is now at ${(0, stepIndexUtils_1.pathToStepIndex)(newCursor)}. Refetch test context for updated step indices.`,
            });
        }
        const error = eventHandler.deleteStepAtPath(startPath);
        if (error) {
            return Promise.resolve({
                success: false,
                errorMessage: error,
            });
        }
        const newCursor = stateProvider.getNextStepIndex();
        return Promise.resolve({
            success: true,
            message: `Deleted step at position ${startLabel}. Cursor is now at ${(0, stepIndexUtils_1.pathToStepIndex)(newCursor)}. Refetch test context for updated step indices.`,
        });
    },
};
