/**
 * MK2 Geometry Validator
 * Inspects actual blessed nodes and validates bounds
 */
import fs from 'fs';
const logPath = '/tmp/mk2_layout.log';
/**
 * Validate screen geometry - inspect actual blessed nodes
 */
export async function validateScreenGeometry(screen, safeW, safeH) {
    const report = {
        maxRightEdge: 0,
        maxBottomEdge: 0,
        violations: [],
    };
    const children = screen.children || [];
    const allElements = [];
    for (const child of children) {
        const left = child.aleft ?? child.left ?? 0;
        const top = child.atop ?? child.top ?? 0;
        const width = child.width ?? 0;
        const height = child.height ?? 0;
        const rightEdge = left + width - 1;
        const bottomEdge = top + height - 1;
        const name = child.type || child.name || 'unknown';
        const type = child.type || 'unknown';
        allElements.push({
            name,
            left,
            top,
            width,
            height,
            rightEdge,
            bottomEdge,
            type,
        });
        // Update max edges
        if (rightEdge > report.maxRightEdge) {
            report.maxRightEdge = rightEdge;
        }
        if (bottomEdge > report.maxBottomEdge) {
            report.maxBottomEdge = bottomEdge;
        }
        // Check violations - use strict bounds (must leave 1 cell buffer)
        // For CloudShell: safeW already accounts for RIGHT=3, so maxRight = safeW - 1
        const maxRightAllowed = safeW - 1; // Leave 1 cell buffer
        const maxBottomAllowed = safeH - 1; // Leave 1 cell buffer
        if (rightEdge > maxRightAllowed || bottomEdge > maxBottomAllowed) {
            report.violations.push({
                name,
                left,
                top,
                width,
                height,
                rightEdge,
                bottomEdge,
            });
        }
    }
    // Log to file with full details
    logGeometry(report, safeW, safeH, allElements);
    return report;
}
/**
 * Log geometry to file
 */
function logGeometry(report, safeW, safeH, allElements) {
    try {
        const timestamp = new Date().toISOString();
        let log = `\n========== [${timestamp}] Geometry Report ==========\n`;
        log += `Safe bounds: ${safeW}x${safeH}\n`;
        log += `Max right edge: ${report.maxRightEdge} (allowed: ${safeW - 2}) ${report.maxRightEdge > safeW - 2 ? '❌ OVERFLOW' : '✅ OK'}\n`;
        log += `Max bottom edge: ${report.maxBottomEdge} (allowed: ${safeH - 2}) ${report.maxBottomEdge > safeH - 2 ? '❌ OVERFLOW' : '✅ OK'}\n`;
        if (report.violations.length > 0) {
            log += `\n❌ VIOLATIONS (${report.violations.length}):\n`;
            for (const v of report.violations) {
                log += `  ${v.name}: x=${v.left} y=${v.top} w=${v.width} h=${v.height} → right=${v.rightEdge} bottom=${v.bottomEdge}\n`;
                log += `    Overflow: ${v.rightEdge > safeW - 2 ? `RIGHT +${v.rightEdge - (safeW - 2)}` : ''} ${v.bottomEdge > safeH - 2 ? `BOTTOM +${v.bottomEdge - (safeH - 2)}` : ''}\n`;
            }
        }
        else {
            log += `\n✅ No violations detected.\n`;
        }
        log += `\nALL ELEMENTS (${allElements.length}):\n`;
        // Sort by rightEdge descending to see widest elements first
        const sorted = [...allElements].sort((a, b) => b.rightEdge - a.rightEdge);
        for (const el of sorted.slice(0, 10)) { // Top 10 widest
            log += `  ${el.name} (${el.type}): x=${el.left} y=${el.top} w=${el.width} h=${el.height} → right=${el.rightEdge} bottom=${el.bottomEdge}\n`;
        }
        log += '==========================================\n\n';
        fs.appendFileSync(logPath, log);
    }
    catch (error) {
        // Try to log the error
        try {
            fs.appendFileSync(logPath, `\n[ERROR] Failed to write geometry log: ${error}\n\n`);
        }
        catch {
            // Ignore
        }
    }
}
/**
 * Get geometry log path
 */
export function getGeometryLogPath() {
    return logPath;
}
//# sourceMappingURL=geometryValidator.js.map