/**
 * MK2 Layout Engine - Deterministic layout computation
 *
 * Fixed layout engine that ensures no overflow:
 * - All boxes must satisfy: x + w <= safeW (so maxX = x + w - 1 <= safeW - 1)
 * - Accounts for gutters and borders
 * - Supports wide (3-column), compact (2-column), and narrow (1-column) layouts
 */
import { detectTerminalProfile } from '../terminalProfile.js';
import { clampRect } from './clampRect.js';
import { MIN_PANEL_W, MIN_PANEL_H, CMD_MIN_W, CMD_MIN_H } from '../safeViewport.js';
// Cache profile to avoid repeated detection
let cachedProfile = null;
// Constants
export const TOP_GUARD = 1; // Reserve top row for debug overlay
export const GUTTER_X = 1; // Horizontal gutter between columns (G)
export const GUTTER_Y = 1; // Vertical gutter between rows (G)
export const BORDER_THICKNESS = 1; // Border thickness (B) - each border adds 1 col/row
// Re-export from safeViewport.ts for backward compatibility
export { MIN_PANEL_W, MIN_PANEL_H, CMD_MIN_W, CMD_MIN_H } from '../safeViewport.js';
/**
 * Get safe margins - uses terminal profile
 */
export function getSafeMargins() {
    const profile = detectTerminalProfile();
    return {
        marginX: profile.safeMarginX,
        marginY: profile.safeMarginY,
    };
}
/**
 * Compute safe canvas dimensions with margins
 * DEPRECATED: Use getSafeViewport() from safeViewport.ts instead
 * Kept for backward compatibility - redirects to viewport system
 * FIXED: Now uses actual getSafeViewport() to get RIGHT=3, BOTTOM=2
 */
export async function getSafeCanvas(cols, rows) {
    // CRITICAL: Use actual viewport system (not hardcoded values)
    // Import dynamically to avoid circular dependency
    const { getSafeViewport } = await import('../safeViewport.js');
    // Create a mock screen object to pass to getSafeViewport
    const mockScreen = { width: cols, height: rows };
    const viewport = getSafeViewport(mockScreen);
    // Return values matching viewport system
    return {
        safeW: viewport.safe.w,
        safeH: viewport.safe.h,
        marginX: viewport.insets.right, // For backward compatibility
        marginY: viewport.insets.bottom, // For backward compatibility
    };
}
/**
 * Hard clamp pass - uses clampRect helper
 * Clamps BEFORE creating widgets, accounting for borders
 */
export function clampLayout(layout) {
    const { safeW, safeH, topGuard } = layout;
    for (const [name, rect] of Object.entries(layout.rects)) {
        const isCommand = name === 'command';
        const clamped = clampRect(rect, safeW, safeH, topGuard, undefined, undefined, isCommand);
        layout.rects[name] = clamped;
    }
}
/**
 * Compute max edges for debug overlay
 */
export function computeMaxEdges(layout) {
    let maxRightEdge = 0;
    let maxBottomEdge = 0;
    // CRITICAL: Blessed box with border: right edge = x + w + 1 (zero-indexed)
    // We validate: x + w <= safeW - 1, so maxRightEdge = x + w (content edge)
    for (const rect of Object.values(layout.rects)) {
        // Skip zero-sized rects (hidden panels)
        if (rect.w === 0 || rect.h === 0) {
            continue;
        }
        // Content edge (not border edge): x + w
        // We check: x + w <= safeW - 1
        const rightEdge = rect.x + rect.w;
        const bottomEdge = rect.y + rect.h;
        if (rightEdge > maxRightEdge) {
            maxRightEdge = rightEdge;
        }
        if (bottomEdge > maxBottomEdge) {
            maxBottomEdge = bottomEdge;
        }
    }
    return { maxRightEdge, maxBottomEdge };
}
/**
 * Validate layout - CRITICAL FIX
 * Acceptance: maxRight <= safeW - 2, maxBottom <= safeH - 2
 */
export function validateLayout(layout) {
    const errors = [];
    const { safeW, safeH, topGuard, cols, rows, safeMarginX, safeMarginY } = layout;
    // Compute max edges
    const { maxRightEdge, maxBottomEdge } = computeMaxEdges(layout);
    layout.maxRightEdge = maxRightEdge;
    layout.maxBottomEdge = maxBottomEdge;
    // CRITICAL: HARD RULE - x + w <= safeW - 1, y + h <= safeH - 1
    // Blessed box with border: right edge = x + w + 1 (zero-indexed)
    // Must enforce: maxRightEdge <= safeW - 1, maxBottomEdge <= safeH - 1
    const maxRightAllowed = safeW - 1; // Leave 1 cell buffer
    const maxBottomAllowed = safeH - 1; // Leave 1 cell buffer (topGuard already accounted in maxBottomEdge)
    if (maxRightEdge > maxRightAllowed) {
        errors.push(`FAIL: maxRight=${maxRightEdge} > ${maxRightAllowed} (safeW=${safeW})`);
    }
    if (maxBottomEdge > maxBottomAllowed) {
        errors.push(`FAIL: maxBottom=${maxBottomEdge} > ${maxBottomAllowed} (safeH=${safeH})`);
    }
    // Log to file (not stdout)
    try {
        const fs = require('fs');
        const log = `[MK2] cols=${cols} rows=${rows} marginX=${safeMarginX} marginY=${safeMarginY} safe=${safeW}x${safeH} maxRight=${maxRightEdge}/<=${maxRightAllowed} maxBottom=${maxBottomEdge}/<=${maxBottomAllowed}\n`;
        fs.appendFileSync('/tmp/mk2_layout.log', log);
    }
    catch { }
    return { errors, clamped: false };
}
/**
 * Compute wide 3-column layout
 * Guarantees: leftX + leftW + GUTTER_X + centerW + GUTTER_X + rightW <= safeW
 */
function computeWideLayout(cols, rows, safeW, safeH) {
    // FIXED: Fixed column widths with proper clamping
    // Ensure columns always sum correctly: leftW + gutter + centerW + gutter + rightW <= safeW
    const MIN_LEFT = 24;
    const MIN_RIGHT = 24;
    const MIN_CENTER = 40;
    const GUTTER = GUTTER_X; // 1 column between panels
    // Calculate widths as percentages of safeW, clamped to minimums
    let leftW = Math.max(MIN_LEFT, Math.floor(safeW * 0.18));
    let rightW = Math.max(MIN_RIGHT, Math.floor(safeW * 0.16));
    // Total gutters between 3 columns = 2 * GUTTER
    const totalGutters = 2 * GUTTER;
    let centerW = safeW - leftW - rightW - totalGutters;
    // If center too narrow, reduce left/right proportionally
    if (centerW < MIN_CENTER) {
        const excess = MIN_CENTER - centerW;
        // Reduce left and right proportionally
        const totalSide = leftW + rightW;
        if (totalSide > 0) {
            const leftReduction = Math.floor(excess * (leftW / totalSide));
            const rightReduction = excess - leftReduction;
            leftW = Math.max(MIN_LEFT, leftW - leftReduction);
            rightW = Math.max(MIN_RIGHT, rightW - rightReduction);
        }
        else {
            // Fallback: equal reduction
            leftW = Math.max(MIN_LEFT, leftW - Math.floor(excess / 2));
            rightW = Math.max(MIN_RIGHT, rightW - Math.ceil(excess / 2));
        }
        centerW = safeW - leftW - rightW - totalGutters;
    }
    // Final check: if still too narrow, this layout won't work
    if (centerW < MIN_CENTER) {
        throw new Error(`Center too narrow for wide layout: centerW=${centerW} (safeW=${safeW}, leftW=${leftW}, rightW=${rightW}, gutters=${totalGutters})`);
    }
    // Verify invariant: leftW + GUTTER + centerW + GUTTER + rightW = safeW
    const totalWidth = leftW + GUTTER + centerW + GUTTER + rightW;
    if (totalWidth > safeW) {
        throw new Error(`Layout width overflow: ${totalWidth} > ${safeW} (left=${leftW} center=${centerW} right=${rightW} gutters=${totalGutters})`);
    }
    // Set x positions with gutters
    const leftX = 0;
    const centerX = leftX + leftW + GUTTER_X;
    const rightX = centerX + centerW + GUTTER_X;
    // Verify invariant: rightX + rightW <= safeW
    if (rightX + rightW > safeW) {
        throw new Error(`Layout overflow: rightX=${rightX} + rightW=${rightW} = ${rightX + rightW} > safeW=${safeW}`);
    }
    // Verify: rightX + RIGHT_W = LEFT_W + GUTTER_X + centerW + GUTTER_X + RIGHT_W
    //        = LEFT_W + RIGHT_W + centerW + GUTTER_X * 2
    //        = safeW - 1 <= safeW ✓
    // Input bar: must be included in safeH math
    // CRITICAL: Command box must have minimum height CMD_MIN_H
    const cmdH = Math.max(CMD_MIN_H, 3);
    const inputGutter = GUTTER_Y;
    const contentH = safeH - cmdH - inputGutter; // Available height for content
    // Left column: stack 3 boxes with gutters
    const stackGutters = 2 * GUTTER_Y;
    const leftAvailableH = contentH - stackGutters;
    const MIN_H = 5;
    const postureH = Math.max(MIN_H, Math.floor(leftAvailableH * 0.30));
    const resourcesH = Math.max(MIN_H, Math.floor(leftAvailableH * 0.30));
    const assetsH = Math.max(MIN_H, leftAvailableH - postureH - resourcesH);
    // Right column: stack 2 boxes with gutter
    const rightStackGutters = 1 * GUTTER_Y;
    const rightAvailableH = contentH - rightStackGutters;
    const networkH = Math.max(MIN_H, Math.floor(rightAvailableH * 0.45));
    const capabilitiesH = Math.max(MIN_H, rightAvailableH - networkH);
    // Center: full height
    const operationsH = contentH;
    // Y positions with gutters
    const leftY = TOP_GUARD;
    const centerY = TOP_GUARD;
    const rightY = TOP_GUARD;
    // Input bar position: contentH + GUTTER_Y
    const cmdY = TOP_GUARD + contentH + inputGutter;
    // Command box must account for borders and enforce minimum width
    // CRITICAL: cmdW must be >= CMD_MIN_W and <= safeW - 1 (leave 1 cell buffer)
    const cmdW = Math.max(CMD_MIN_W, safeW - 1); // Account for borders + 1 cell buffer
    // Verify bottom edge: cmdY + cmdH + borders <= safeH
    // cmdY + cmdH + 2 <= safeH + TOP_GUARD
    if (cmdY + cmdH + 2 > safeH + TOP_GUARD) {
        throw new Error(`Input bar overflow: cmdY=${cmdY} cmdH=${cmdH} safeH=${safeH}`);
    }
    // Calculate y positions for stacked boxes
    const resourcesY = leftY + postureH + GUTTER_Y;
    const assetsY = resourcesY + resourcesH + GUTTER_Y;
    const capabilitiesY = rightY + networkH + GUTTER_Y;
    // Verify invariants
    if (assetsY + assetsH > contentH + TOP_GUARD) {
        throw new Error(`Left column overflow: assetsY=${assetsY} + assetsH=${assetsH} > contentH=${contentH}`);
    }
    if (capabilitiesY + capabilitiesH > contentH + TOP_GUARD) {
        throw new Error(`Right column overflow: capabilitiesY=${capabilitiesY} + capabilitiesH=${capabilitiesH} > contentH=${contentH}`);
    }
    const rects = {
        posture: { x: leftX, y: leftY, w: leftW, h: postureH },
        resources: { x: leftX, y: resourcesY, w: leftW, h: resourcesH },
        assets: { x: leftX, y: assetsY, w: leftW, h: assetsH },
        operations: { x: centerX, y: centerY, w: centerW, h: operationsH },
        network: { x: rightX, y: rightY, w: rightW, h: networkH },
        capabilities: { x: rightX, y: capabilitiesY, w: rightW, h: capabilitiesH },
        command: { x: 0, y: cmdY, w: cmdW, h: cmdH },
    };
    // Get safe margins from profile
    if (!cachedProfile) {
        cachedProfile = detectTerminalProfile();
    }
    const layout = {
        mode: 'wide',
        safeW, safeH, cols, rows,
        leftW, centerW, rightW,
        leftX, centerX, rightX,
        cmdH, usableH: contentH,
        topGuard: TOP_GUARD,
        rightGuard: 0,
        bottomGuard: 0,
        safeMarginX: cachedProfile.safeMarginX,
        safeMarginY: cachedProfile.safeMarginY,
        maxRightEdge: 0,
        maxBottomEdge: 0,
        rects,
    };
    clampLayout(layout);
    return layout;
}
/**
 * Compute compact 2-column layout
 */
function computeCompactLayout(cols, rows, safeW, safeH) {
    // Percent-based with minimums
    const MIN_LEFT = 30;
    const leftW = Math.max(MIN_LEFT, Math.floor(safeW * 0.35));
    const gutter = GUTTER_X;
    const rightW = safeW - leftW - gutter; // Remaining space after left + gutter
    const leftX = 0;
    const rightX = leftX + leftW + gutter;
    // Verify invariant
    if (rightX + rightW > safeW) {
        throw new Error(`Compact layout overflow: rightX=${rightX} + rightW=${rightW} = ${rightX + rightW} > safeW=${safeW}`);
    }
    // Input bar: positioned at bottom with gutter
    // CRITICAL: Command box must have minimum dimensions
    const cmdH = Math.max(CMD_MIN_H, 3);
    const inputGutter = GUTTER_Y;
    const contentH = safeH - cmdH - inputGutter;
    // CRITICAL: cmdW must be >= CMD_MIN_W and <= safeW - 1 (leave 1 cell buffer)
    const cmdW = Math.max(CMD_MIN_W, safeW - 1);
    const cmdY = TOP_GUARD + contentH + inputGutter;
    // Verify input bar doesn't overflow (accounting for borders)
    if (cmdY + cmdH + 2 > safeH + TOP_GUARD) {
        throw new Error(`Input bar overflow: cmdY=${cmdY} + cmdH=${cmdH} + borders > safeH=${safeH}`);
    }
    const leftY = TOP_GUARD;
    const rightY = TOP_GUARD;
    // Left column: stack 3 boxes with gutters
    const leftStackGutters = 2 * GUTTER_Y;
    const leftAvailableH = contentH - leftStackGutters;
    const MIN_H = 5;
    const postureH = Math.max(MIN_H, Math.floor(leftAvailableH * 0.30));
    const resourcesH = Math.max(MIN_H, Math.floor(leftAvailableH * 0.30));
    const assetsH = Math.max(MIN_H, leftAvailableH - postureH - resourcesH);
    // Right column: stack 3 boxes with gutters
    const rightStackGutters = 2 * GUTTER_Y;
    const rightAvailableH = contentH - rightStackGutters;
    const operationsH = Math.max(MIN_H, Math.floor(rightAvailableH * 0.40));
    const networkH = Math.max(MIN_H, Math.floor(rightAvailableH * 0.30));
    const capabilitiesH = Math.max(MIN_H, rightAvailableH - operationsH - networkH);
    // Calculate Y positions with gutters
    const resourcesY = leftY + postureH + GUTTER_Y;
    const assetsY = resourcesY + resourcesH + GUTTER_Y;
    const networkY = rightY + operationsH + GUTTER_Y;
    const capabilitiesY = networkY + networkH + GUTTER_Y;
    const rects = {
        posture: { x: leftX, y: leftY, w: leftW, h: postureH },
        resources: { x: leftX, y: resourcesY, w: leftW, h: resourcesH },
        assets: { x: leftX, y: assetsY, w: leftW, h: assetsH },
        operations: { x: rightX, y: rightY, w: rightW, h: operationsH },
        network: { x: rightX, y: networkY, w: rightW, h: networkH },
        capabilities: { x: rightX, y: capabilitiesY, w: rightW, h: capabilitiesH },
        command: { x: 0, y: cmdY, w: cmdW, h: cmdH },
    };
    // Get safe margins from profile
    if (!cachedProfile) {
        cachedProfile = detectTerminalProfile();
    }
    const layout = {
        mode: 'compact',
        safeW, safeH, cols, rows,
        leftW, centerW: 0, rightW,
        leftX, centerX: 0, rightX,
        cmdH, usableH: contentH,
        topGuard: TOP_GUARD,
        rightGuard: 0,
        bottomGuard: 0,
        safeMarginX: cachedProfile.safeMarginX,
        safeMarginY: cachedProfile.safeMarginY,
        maxRightEdge: 0,
        maxBottomEdge: 0,
        rects,
    };
    clampLayout(layout);
    return layout;
}
/**
 * Compute narrow 1-column layout (for very small terminals)
 */
function computeNarrowLayout(cols, rows, safeW, safeH) {
    // Input bar: positioned at bottom with gutter
    // CRITICAL: Command box must have minimum dimensions
    const cmdH = Math.max(CMD_MIN_H, 3);
    const inputGutter = GUTTER_Y;
    const contentH = safeH - cmdH - inputGutter;
    // CRITICAL: cmdW must be >= CMD_MIN_W and <= safeW - 1 (leave 1 cell buffer)
    const cmdW = Math.max(CMD_MIN_W, safeW - 1);
    const cmdY = TOP_GUARD + contentH + inputGutter;
    // Verify input bar doesn't overflow (accounting for borders)
    if (cmdY + cmdH + 2 > safeH + TOP_GUARD) {
        throw new Error(`Input bar overflow: cmdY=${cmdY} + cmdH=${cmdH} + borders > safeH=${safeH}`);
    }
    const usableH = contentH;
    // Single column, account for borders
    const colW = safeW - 2; // Account for borders (left + right = 2)
    const colX = 0;
    // Stack all panels vertically
    // Operations gets more space (1.5x), others share remaining space
    const operationsH = Math.floor(usableH * 0.3); // 30% for operations
    const remainingH = usableH - operationsH - GUTTER_Y * 5;
    const smallPanelH = Math.floor(remainingH / 5); // 5 other panels
    let currentY = TOP_GUARD;
    const rects = {
        posture: { x: colX, y: currentY, w: colW, h: smallPanelH },
        resources: { x: colX, y: currentY + smallPanelH + GUTTER_Y, w: colW, h: smallPanelH },
        assets: { x: colX, y: currentY + (smallPanelH + GUTTER_Y) * 2, w: colW, h: smallPanelH },
        operations: { x: colX, y: currentY + (smallPanelH + GUTTER_Y) * 3, w: colW, h: operationsH },
        network: { x: colX, y: currentY + (smallPanelH + GUTTER_Y) * 3 + operationsH + GUTTER_Y, w: colW, h: smallPanelH },
        capabilities: { x: colX, y: currentY + (smallPanelH + GUTTER_Y) * 3 + operationsH + GUTTER_Y + smallPanelH + GUTTER_Y, w: colW, h: smallPanelH },
        command: { x: 0, y: cmdY, w: cmdW, h: cmdH },
    };
    const layout = {
        mode: 'narrow',
        safeW, safeH, cols, rows,
        leftW: colW, centerW: 0, rightW: 0,
        leftX: colX, centerX: 0, rightX: 0,
        cmdH, usableH,
        topGuard: TOP_GUARD,
        rightGuard: 0,
        bottomGuard: 0,
        safeMarginX: 0,
        safeMarginY: 0,
        maxRightEdge: 0,
        maxBottomEdge: 0,
        rects,
    };
    clampLayout(layout);
    return layout;
}
/**
 * Compute layout spec from screen dimensions
 * FIXED: Single source of truth - uses getDims() from dimensions.ts
 * NEVER throws "Terminal too small" - that's handled by the caller
 */
export async function computeLayout(cols, rows) {
    // Get dimensions - but DON'T check gate here (caller does that)
    const { safeW, safeH, marginX, marginY } = await getSafeCanvas(cols, rows);
    // Debug output
    try {
        const fs = require('fs');
        const stdoutCols = process.stdout.columns || cols;
        const stdoutRows = process.stdout.rows || rows;
        const debugLine = `[COMPUTE] screen=${cols}x${rows} stdout=${stdoutCols}x${stdoutRows} safe=${safeW}x${safeH} marginX=${marginX} marginY=${marginY}\n`;
        fs.appendFileSync('/tmp/mk2_layout.log', debugLine);
    }
    catch { }
    // Try wide layout (160+ cols, 40+ rows) - use safe dimensions for check
    if (safeW >= 160 && safeH >= 40) {
        try {
            const layout = computeWideLayout(cols, rows, safeW, safeH);
            layout.safeMarginX = marginX;
            layout.safeMarginY = marginY;
            const result = validateLayout(layout);
            // Log validation result for debugging
            if (result.errors.length > 0) {
                try {
                    const fs = require('fs');
                    fs.appendFileSync('/tmp/mk2_layout.log', `[WIDE] Validation warnings: ${result.errors.join(', ')} (allowing anyway)\n`);
                }
                catch { }
            }
            // FIXED: Always return layout if computation succeeded - validation errors are just warnings
            return layout;
        }
        catch (error) {
            // Log error for debugging
            try {
                const fs = require('fs');
                fs.appendFileSync('/tmp/mk2_layout.log', `[WIDE] Exception: ${error instanceof Error ? error.message : String(error)}\n`);
            }
            catch { }
            // Fall through to compact
        }
    }
    // Try compact layout (100+ cols, 30+ rows) - use safe dimensions for check
    if (safeW >= 100 && safeH >= 30) {
        try {
            const layout = computeCompactLayout(cols, rows, safeW, safeH);
            layout.safeMarginX = marginX;
            layout.safeMarginY = marginY;
            const result = validateLayout(layout);
            if (result.errors.length > 0) {
                try {
                    const fs = require('fs');
                    fs.appendFileSync('/tmp/mk2_layout.log', `[COMPACT] Validation warnings: ${result.errors.join(', ')} (allowing anyway)\n`);
                }
                catch { }
            }
            // FIXED: Always return layout if computation succeeded
            return layout;
        }
        catch (error) {
            try {
                const fs = require('fs');
                fs.appendFileSync('/tmp/mk2_layout.log', `[COMPACT] Exception: ${error instanceof Error ? error.message : String(error)}\n`);
            }
            catch { }
            // Fall through to narrow
        }
    }
    // Try narrow layout (90+ cols, 22+ rows) - use safe dimensions for check
    if (safeW >= 90 && safeH >= 22) {
        try {
            const layout = computeNarrowLayout(cols, rows, safeW, safeH);
            layout.safeMarginX = marginX;
            layout.safeMarginY = marginY;
            const result = validateLayout(layout);
            if (result.errors.length > 0) {
                try {
                    const fs = require('fs');
                    fs.appendFileSync('/tmp/mk2_layout.log', `[NARROW] Validation warnings: ${result.errors.join(', ')} (allowing anyway)\n`);
                }
                catch { }
            }
            try {
                const fs = require('fs');
                fs.appendFileSync('/tmp/mk2_layout.log', `[NARROW] SUCCESS: Returning narrow layout\n`);
            }
            catch { }
            // FIXED: Always return layout if computation succeeded
            return layout;
        }
        catch (error) {
            try {
                const fs = require('fs');
                fs.appendFileSync('/tmp/mk2_layout.log', `[NARROW] Exception: ${error instanceof Error ? error.message : String(error)}\nStack: ${error instanceof Error ? error.stack : ''}\n`);
            }
            catch { }
            // Terminal is too small
        }
    }
    // FIXED: All layouts failed - return a minimal fallback layout instead of throwing
    // This should never happen, but if it does, we'll show something rather than crash
    try {
        const fs = require('fs');
        fs.appendFileSync('/tmp/mk2_layout.log', `[ERROR] All layouts failed! safeW=${safeW} safeH=${safeH} cols=${cols} rows=${rows}\n`);
        fs.appendFileSync('/tmp/mk2_layout.log', `[ERROR] Using minimal fallback layout\n`);
    }
    catch { }
    // Return minimal fallback layout - single operations box + command box
    // CRITICAL: Use constants from safeViewport.ts
    const cmdH = Math.max(CMD_MIN_H, 3);
    const inputGutter = GUTTER_Y;
    const contentH = Math.max(10, safeH - cmdH - inputGutter);
    const cmdW = Math.max(CMD_MIN_W, safeW - 1); // Leave 1 cell buffer
    const cmdY = TOP_GUARD + contentH + inputGutter;
    const opsW = Math.max(MIN_PANEL_W, safeW - 1); // Leave 1 cell buffer
    const opsH = Math.max(MIN_PANEL_H, contentH);
    const fallbackLayout = {
        mode: 'narrow',
        safeW,
        safeH,
        cols,
        rows,
        leftW: 0,
        centerW: opsW,
        rightW: 0,
        leftX: 0,
        centerX: 0,
        rightX: 0,
        cmdH,
        usableH: contentH,
        topGuard: TOP_GUARD,
        rightGuard: 0,
        bottomGuard: 0,
        safeMarginX: marginX,
        safeMarginY: marginY,
        rects: {
            operations: { x: 0, y: TOP_GUARD, w: opsW, h: opsH },
            command: { x: 0, y: cmdY, w: cmdW, h: cmdH },
            // Empty rects for other panels (they won't be rendered)
            posture: { x: 0, y: 0, w: 0, h: 0 },
            resources: { x: 0, y: 0, w: 0, h: 0 },
            assets: { x: 0, y: 0, w: 0, h: 0 },
            network: { x: 0, y: 0, w: 0, h: 0 },
            capabilities: { x: 0, y: 0, w: 0, h: 0 },
        },
        maxRightEdge: opsW + 1,
        maxBottomEdge: cmdY + cmdH + 1,
    };
    // Clamp the fallback layout
    clampLayout(fallbackLayout);
    // Log fallback layout
    try {
        const fs = require('fs');
        fs.appendFileSync('/tmp/mk2_layout.log', `[FALLBACK] Returning minimal layout: ops=${opsW}x${opsH} cmd=${cmdW}x${cmdH}\n`);
    }
    catch { }
    return fallbackLayout;
}
//# sourceMappingURL=computeLayout.js.map