/**
 * Phase B: Offender Scanner - Detect any element exceeding safe bounds
 *
 * Traverses screen.children recursively and logs any element where:
 * - rightEdge >= safeCols OR
 * - bottomEdge >= safeRows
 *
 * Logs to file (not stdout) to prevent log overflow from causing scrollbars.
 */
import { fileLogger } from '../log.js';
/**
 * Phase B: Recursive offender scanner
 * Traverses element tree and finds any element exceeding safe bounds
 */
export function scanOffenders(element, safeCols, safeRows, offenders = [], path = []) {
    const elType = element.type || 'unknown';
    const elLeft = typeof element.left === 'number' ? element.left : 0;
    const elTop = typeof element.top === 'number' ? element.top : 0;
    const elWidth = typeof element.width === 'number' ? element.width : 0;
    const elHeight = typeof element.height === 'number' ? element.height : 0;
    // Phase B: Compute rightEdge = left + width - 1 (0-indexed, so right edge is at left + width - 1)
    // Actually, in blessed, if left=0, width=10, the rightmost cell is at column 9 (0-indexed)
    // So rightEdge = left + width (exclusive) means rightEdge = left + width - 1 (inclusive)
    // But for overflow detection, we check if left + width > safeCols (strictly greater)
    const rightEdge = elLeft + elWidth;
    const bottomEdge = elTop + elHeight;
    // Phase B: Check if this element exceeds bounds
    if (rightEdge > safeCols || bottomEdge > safeRows) {
        const contentPreview = typeof element.content === 'string'
            ? element.content.substring(0, 50).replace(/\n/g, '\\n')
            : 'no-content';
        const currentPath = [...path, elType];
        offenders.push({
            type: elType,
            left: elLeft,
            top: elTop,
            width: elWidth,
            height: elHeight,
            rightEdge,
            bottomEdge,
            contentPreview,
            path: currentPath,
        });
    }
    // Phase B: Recurse into children
    const children = element.children || [];
    for (const child of children) {
        scanOffenders(child, safeCols, safeRows, offenders, [...path, elType]);
    }
    return offenders;
}
/**
 * Phase B: Main scanner function - logs offenders to file
 */
export function scanAndLogOffenders(screen, safeCols, safeRows) {
    const offenders = scanOffenders(screen, safeCols, safeRows);
    if (offenders.length > 0) {
        fileLogger.force(`[OFFENDER_SCANNER] Found ${offenders.length} elements exceeding safe bounds (safeCols=${safeCols} safeRows=${safeRows}):`);
        offenders.slice(0, 20).forEach((off, idx) => {
            const exceedsCols = off.rightEdge > safeCols;
            const exceedsRows = off.bottomEdge > safeRows;
            const overflowCols = exceedsCols ? ` OVERFLOW COLS: rightEdge=${off.rightEdge} > safeCols=${safeCols} (exceeds by ${off.rightEdge - safeCols})` : '';
            const overflowRows = exceedsRows ? ` OVERFLOW ROWS: bottomEdge=${off.bottomEdge} > safeRows=${safeRows} (exceeds by ${off.bottomEdge - safeRows})` : '';
            fileLogger.force(`[OFFENDER ${idx + 1}] type=${off.type} ` +
                `left=${off.left} top=${off.top} width=${off.width} height=${off.height} ` +
                `rightEdge=${off.rightEdge} bottomEdge=${off.bottomEdge}` +
                `${overflowCols}${overflowRows} ` +
                `path=${off.path.join(' > ')} ` +
                `content="${off.contentPreview}"`);
        });
        if (offenders.length > 20) {
            fileLogger.force(`[OFFENDER_SCANNER] ... and ${offenders.length - 20} more offenders`);
        }
    }
    else {
        fileLogger.force(`[OFFENDER_SCANNER] ✓ No offenders found - all elements within safe bounds (safeCols=${safeCols} safeRows=${safeRows})`);
    }
    return offenders;
}
//# sourceMappingURL=offenderScanner.js.map