"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateMoveCursorTarget = validateMoveCursorTarget;
const mablscript_step_types_1 = require("../sharedUtils/mablscript-step-types");
const stepIndexUtils_1 = require("../sharedUtils/stepIndexUtils");
function validateMoveCursorTarget(steps, getNestedSteps, targetPath) {
    const parentPath = targetPath.slice(0, -1);
    if (parentPath.length > 0) {
        const parent = (0, stepIndexUtils_1.getStepAtPath)(steps, parentPath, getNestedSteps);
        if (!parent ||
            !((0, mablscript_step_types_1.isStepGroupStep)(parent) || (0, mablscript_step_types_1.isStepWithReusableFlow)(parent))) {
            return {
                success: false,
                errorMessage: `Invalid position: no group or flow at ${(0, stepIndexUtils_1.pathToStepIndex)(parentPath)}`,
            };
        }
    }
    let stepsAtLevel = steps;
    for (const index of parentPath) {
        const step = stepsAtLevel[index];
        const nested = getNestedSteps(step);
        if (nested) {
            stepsAtLevel = nested;
        }
    }
    const targetIndex = targetPath[targetPath.length - 1];
    if (targetIndex > stepsAtLevel.length) {
        const failingSegment = targetPath.length;
        const failingSegmentLabel = targetPath.length === 1
            ? 'top-level step'
            : `step under ${(0, stepIndexUtils_1.pathToStepIndex)(targetPath.slice(0, -1))}`;
        return {
            success: false,
            errorMessage: `Position ${(0, stepIndexUtils_1.pathToStepIndex)(targetPath)} is out of bounds — segment ${failingSegment} requested ${targetIndex + 1}, but there are only ${stepsAtLevel.length} ${failingSegmentLabel}s (max position ${stepsAtLevel.length}).`,
        };
    }
    return { success: true };
}
