/**
 * MK1 Safe Viewport Calculator
 *
 * Computes safeCols and safeRows that all layout uses.
 * Subtracts configurable margins to prevent overflow in problematic terminals.
 */
import { logger } from '../logger.js';
/**
 * Phase A: Get Safe Viewport - Single Source of Truth
 *
 * Rule: last usable column = cols - RIGHT_GAP
 *       last usable row = rows - BOTTOM_GAP
 *
 * Conservative gaps during debugging to kill scrollbars:
 * - RIGHT_GAP = 6 (can reduce later once offenders are gone)
 * - BOTTOM_GAP = 2
 *
 * Hard clamps: safeCols >= MIN_COLS, safeRows >= MIN_ROWS
 */
export function getSafeViewport(screenCols, screenRows) {
    // Phase A: Conservative gaps for debugging (can tighten later)
    const MIN_COLS = 40;
    const MIN_ROWS = 20;
    // Read environment variables with conservative defaults
    // INCREASED RIGHT_GAP: Add more margin to eliminate horizontal scrollbar
    const RIGHT_GAP = process.env.TUI_SAFE_PAD_X ? Number(process.env.TUI_SAFE_PAD_X) :
        process.env.TUI_SAFE_RIGHT ? Number(process.env.TUI_SAFE_RIGHT) : 8; // Increased from 6 to 8 for more safety
    const BOTTOM_GAP = process.env.TUI_SAFE_PAD_Y ? Number(process.env.TUI_SAFE_PAD_Y) :
        process.env.TUI_SAFE_BOTTOM ? Number(process.env.TUI_SAFE_BOTTOM) : 2; // Conservative: 2 for debugging
    // Check for preset (overrides)
    const safePreset = process.env.TUI_SAFE_PRESET;
    let rightMargin = RIGHT_GAP;
    let bottomMargin = BOTTOM_GAP;
    if (safePreset === 'aws') {
        rightMargin = 6; // Keep conservative during debug
        bottomMargin = 2;
    }
    // Phase A: Compute safe viewport with hard clamps
    const safeCols = Math.max(MIN_COLS, screenCols - rightMargin);
    const safeRows = Math.max(MIN_ROWS, screenRows - bottomMargin);
    // Phase A: Hard assertions to catch math bugs
    if (safeCols > screenCols) {
        throw new Error(`[TUI] safeCols > screenCols (math bug): safeCols=${safeCols} screenCols=${screenCols} RIGHT_GAP=${rightMargin}`);
    }
    if (safeRows > screenRows) {
        throw new Error(`[TUI] safeRows > screenRows (math bug): safeRows=${safeRows} screenRows=${screenRows} BOTTOM_GAP=${bottomMargin}`);
    }
    // Phase A: Log safe viewport for debugging (to file, not stdout)
    // Use fileLogger here to avoid stdout corruption - import dynamically to avoid circular deps
    import('../log.js').then(({ fileLogger }) => {
        fileLogger.force(`[SAFE_VIEWPORT] cols=${screenCols} rows=${screenRows} ` +
            `safeCols=${safeCols} safeRows=${safeRows} ` +
            `RIGHT_GAP=${rightMargin} BOTTOM_GAP=${bottomMargin}`);
    }).catch(() => {
        // Fallback to logger if fileLogger not available yet
        if (logger) {
            logger.debug(`[SAFE_VIEWPORT] cols=${screenCols} rows=${screenRows} ` +
                `safeCols=${safeCols} safeRows=${safeRows} ` +
                `RIGHT_GAP=${rightMargin} BOTTOM_GAP=${bottomMargin}`);
        }
    });
    return {
        safeCols,
        safeRows,
        rightMargin,
        bottomMargin,
        screenCols,
        screenRows,
    };
}
//# sourceMappingURL=safeViewport.js.map