"use strict";
/**
 * Route Discovery Utilities
 *
 * Shared helpers used across route discovery, scoring, and validation.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.findClosestRoute = findClosestRoute;
exports.extractBlock = extractBlock;
/**
 * Find the route whose handler line is closest to (and at or before) the finding line.
 * Falls back to the closest route overall if none are before the finding.
 */
function findClosestRoute(routes, findingLine) {
    let best = null;
    let bestDistance = Infinity;
    for (const route of routes) {
        const distance = findingLine - route.handlerLine;
        if (distance >= 0 && distance < bestDistance) {
            best = route;
            bestDistance = distance;
        }
    }
    // If no route is before the finding, return the closest one overall
    if (!best && routes.length > 0) {
        best = routes.reduce((a, b) => Math.abs(a.handlerLine - findingLine) < Math.abs(b.handlerLine - findingLine) ? a : b);
    }
    return best;
}
/**
 * Extract a brace-delimited block starting from a given line.
 * Tracks brace depth to find the matching closing brace.
 * Falls back to 50-line limit if no closing brace is found.
 */
function extractBlock(lines, startLine) {
    let depth = 0;
    let started = false;
    const result = [];
    for (let i = startLine; i < Math.min(startLine + 50, lines.length); i++) {
        const line = lines[i];
        result.push(line);
        for (const ch of line) {
            if (ch === '{') {
                depth++;
                started = true;
            }
            if (ch === '}')
                depth--;
        }
        if (started && depth <= 0)
            break;
    }
    return result.join('\n');
}
//# sourceMappingURL=utils.js.map