/**
 * MK2 Phase 1B: Box Renderer - Single Source of Truth
 *
 * Creates blessed boxes from layout spec
 * No widget manager, no visibility hacks
 */
import blessed from 'neo-blessed';
import { assertBoxFits } from './invariants.js';
/**
 * Create a blessed box with hard invariant checks (MK2 Hotfix: Pass topGuard)
 */
export function mkBox(screen, rect, label, safeW, safeH, topGuard = 0, disableBorders = false // Not used anymore, but kept for compatibility
) {
    // Skip if box is too small (clamped to zero) - check BEFORE assertBoxFits
    if (rect.w <= 0 || rect.h <= 0) {
        // Return a minimal invisible box to avoid errors
        try {
            const fs = require('fs');
            fs.appendFileSync('/tmp/mk2_layout.log', `[mkBox] Skipping ${label}: w=${rect.w} h=${rect.h} (zero-sized)\n`);
        }
        catch { }
        return blessed.box({
            parent: screen,
            top: 0,
            left: 0,
            width: 1,
            height: 1,
            hidden: true,
        });
    }
    // Hard invariant check - but don't throw, just log and clamp
    try {
        assertBoxFits(rect, safeW, safeH, topGuard);
    }
    catch (error) {
        // Log the violation but don't fail - clamp the rect instead
        try {
            const fs = require('fs');
            const errorMsg = error instanceof Error ? error.message : String(error);
            fs.appendFileSync('/tmp/mk2_layout.log', `[mkBox] ${label} failed assertBoxFits: ${errorMsg}\n`);
            fs.appendFileSync('/tmp/mk2_layout.log', `[mkBox] ${label} rect: x=${rect.x} y=${rect.y} w=${rect.w} h=${rect.h}\n`);
        }
        catch { }
        // Clamp the rect to fit
        rect.x = Math.max(0, Math.min(rect.x, safeW - rect.w - 2));
        rect.y = Math.max(topGuard, Math.min(rect.y, safeH + topGuard - rect.h - 2));
        rect.w = Math.max(3, Math.min(rect.w, safeW - rect.x - 2));
        rect.h = Math.max(3, Math.min(rect.h, safeH + topGuard - rect.y - 2));
    }
    // Ensure minimum size for visibility
    const boxW = Math.max(rect.w, 3);
    const boxH = Math.max(rect.h, 3);
    const boxX = Math.max(0, rect.x);
    const boxY = Math.max(0, rect.y);
    // Create box with proper border styling
    const box = blessed.box({
        parent: screen,
        top: boxY,
        left: boxX,
        width: boxW,
        height: boxH,
        border: { type: 'line' }, // Always use borders - layout accounts for them
        label: ` ${label} `,
        content: ` ${label} `, // Set content so box is visible
        tags: false,
        style: {
            fg: 'cyan',
            border: { fg: 'cyan' },
            // No background fill - let terminal default show through
        },
    });
    // Log box creation for debugging
    try {
        const fs = require('fs');
        fs.appendFileSync('/tmp/mk2_layout.log', `[mkBox] Created ${label}: x=${boxX} y=${boxY} w=${boxW} h=${boxH}\n`);
    }
    catch { }
    return box;
}
/**
 * Apply layout - create all boxes from spec
 * Handles wide (3-column), compact (2-column), and narrow (1-column) modes
 */
export function applyLayout(screen, spec) {
    const boxes = [];
    // Log layout mode and rects for debugging
    try {
        const fs = require('fs');
        fs.appendFileSync('/tmp/mk2_layout.log', `[applyLayout] Mode: ${spec.mode}, safeW=${spec.safeW} safeH=${spec.safeH} topGuard=${spec.topGuard}\n`);
        fs.appendFileSync('/tmp/mk2_layout.log', `[applyLayout] Rects: posture=${spec.rects.posture.w}x${spec.rects.posture.h}, ops=${spec.rects.operations.w}x${spec.rects.operations.h}, caps=${spec.rects.capabilities.w}x${spec.rects.capabilities.h}\n`);
    }
    catch { }
    // Create boxes - wrap in try/catch so one failure doesn't stop all boxes
    const createBox = (rect, label) => {
        try {
            const box = mkBox(screen, rect, label, spec.safeW, spec.safeH, spec.topGuard);
            return box;
        }
        catch (error) {
            try {
                const fs = require('fs');
                const errorMsg = error instanceof Error ? error.message : String(error);
                fs.appendFileSync('/tmp/mk2_layout.log', `[applyLayout] Failed to create ${label}: ${errorMsg}\n`);
            }
            catch { }
            return null;
        }
    };
    if (spec.mode === 'wide') {
        // Wide: 3 columns (left: posture/resources/assets, center: operations, right: network/capabilities)
        const posture = createBox(spec.rects.posture, 'POSTURE');
        const resources = createBox(spec.rects.resources, 'RESOURCES');
        const assets = createBox(spec.rects.assets, 'ASSETS');
        const operations = createBox(spec.rects.operations, 'OPERATIONS');
        const network = createBox(spec.rects.network, 'NETWORK');
        const capabilities = createBox(spec.rects.capabilities, 'CAPABILITIES');
        if (posture)
            boxes.push(posture);
        if (resources)
            boxes.push(resources);
        if (assets)
            boxes.push(assets);
        if (operations)
            boxes.push(operations);
        if (network)
            boxes.push(network);
        if (capabilities)
            boxes.push(capabilities);
    }
    else if (spec.mode === 'compact') {
        // Compact: 2 columns (left: posture/resources/assets, right: operations/network/capabilities)
        const posture = createBox(spec.rects.posture, 'POSTURE');
        const resources = createBox(spec.rects.resources, 'RESOURCES');
        const assets = createBox(spec.rects.assets, 'ASSETS');
        const operations = createBox(spec.rects.operations, 'OPERATIONS');
        const network = createBox(spec.rects.network, 'NETWORK');
        const capabilities = createBox(spec.rects.capabilities, 'CAPABILITIES');
        if (posture)
            boxes.push(posture);
        if (resources)
            boxes.push(resources);
        if (assets)
            boxes.push(assets);
        if (operations)
            boxes.push(operations);
        if (network)
            boxes.push(network);
        if (capabilities)
            boxes.push(capabilities);
    }
    else {
        // Narrow: 1 column (all panels stacked)
        const posture = createBox(spec.rects.posture, 'POSTURE');
        const resources = createBox(spec.rects.resources, 'RESOURCES');
        const assets = createBox(spec.rects.assets, 'ASSETS');
        const operations = createBox(spec.rects.operations, 'OPERATIONS');
        const network = createBox(spec.rects.network, 'NETWORK');
        const capabilities = createBox(spec.rects.capabilities, 'CAPABILITIES');
        if (posture)
            boxes.push(posture);
        if (resources)
            boxes.push(resources);
        if (assets)
            boxes.push(assets);
        if (operations)
            boxes.push(operations);
        if (network)
            boxes.push(network);
        if (capabilities)
            boxes.push(capabilities);
    }
    // Log how many boxes were created
    try {
        const fs = require('fs');
        fs.appendFileSync('/tmp/mk2_layout.log', `[applyLayout] Created ${boxes.length} boxes\n`);
    }
    catch { }
    // Note: COMMAND is created separately as a textbox (see mk2App.ts)
    return boxes;
}
//# sourceMappingURL=applyLayout.js.map