/**
 * MK2 Safe Viewport - Single Source of Truth for Drawable Area
 *
 * Prevents scrollbars in CloudShell/browser terminals by enforcing insets
 * that prevent drawing on the last row/column.
 *
 * ALL dimension constants are defined here as the single source of truth.
 */
// ============================================================================
// DIMENSION CONSTANTS - SINGLE SOURCE OF TRUTH
// ============================================================================
/** Guard columns on the right (minimum 1, but 3 for web terminals) */
export const GUARD_RIGHT_WEB = 3; // CloudShell/browser: never draw on last 3 columns
export const GUARD_RIGHT_LOCAL = 0; // Local: use full width
/** Guard rows on the bottom (minimum 1) */
export const GUARD_BOTTOM_WEB = 2; // CloudShell/browser: never draw on last 2 rows
export const GUARD_BOTTOM_LOCAL = 0; // Local: use full height
/** Guard row on top (reserve for debug overlay) */
export const GUARD_TOP = 1; // Always reserve top row
/** Guard columns on left (none needed) */
export const GUARD_LEFT = 0; // No left guard
/** Minimum required dimensions for layout */
export const MIN_COLS = 90; // Minimum safe columns
export const MIN_ROWS = 22; // Minimum safe rows
/** Panel minimum dimensions */
export const MIN_PANEL_W = 20; // Minimum panel width
export const MIN_PANEL_H = 5; // Minimum panel height
/** Command box minimum dimensions */
export const CMD_MIN_W = 10; // Minimum command box width
export const CMD_MIN_H = 3; // Minimum command box height (must be >= 3)
/**
 * Detect if running in a web/browser terminal (CloudShell, etc.)
 *
 * Returns true if:
 * - TUI_WEB=1 (manual override)
 * - AWS_EXECUTION_ENV exists (AWS CloudShell)
 * - CLOUDSHELL exists
 */
export function isWebTerminal() {
    // Manual override
    if (process.env.TUI_WEB === '1') {
        return true;
    }
    // AWS CloudShell detection
    if (process.env.AWS_EXECUTION_ENV || process.env.CLOUDSHELL) {
        return true;
    }
    // AWS region env vars (fallback detection)
    if (process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION) {
        return true;
    }
    return false;
}
/**
 * Get safe viewport for the terminal
 *
 * CloudShell/browser mode insets:
 * - right = 3 (never draw on last 3 columns - browser DOM needs extra buffer)
 * - bottom = 2 (never draw on last 2 rows - prevents vertical scrollbar)
 * - top = 1 (reserve for debug overlay)
 * - left = 0 (no left inset needed)
 *
 * Local mode insets:
 * - right = 0 (use full width)
 * - bottom = 0 (use full height)
 * - top = 1 (reserve for debug overlay)
 * - left = 0
 */
export function getSafeViewport(screen) {
    const cols = screen.width || 80;
    const rows = screen.height || 24;
    const isWeb = isWebTerminal();
    // Define insets based on terminal type
    // CRITICAL: CloudShell needs RIGHT=3 to avoid scrollbars (subpixel/DOM issues)
    const insets = isWeb
        ? { left: GUARD_LEFT, top: GUARD_TOP, right: GUARD_RIGHT_WEB, bottom: GUARD_BOTTOM_WEB }
        : { left: GUARD_LEFT, top: GUARD_TOP, right: GUARD_RIGHT_LOCAL, bottom: GUARD_BOTTOM_LOCAL };
    // Compute safe drawable area with minimum guarantees
    const safeX = insets.left;
    const safeY = insets.top;
    // CRITICAL: safeW/safeH must never be 0 - enforce minimum of 1
    const safeW = Math.max(1, cols - insets.left - insets.right);
    const safeH = Math.max(1, rows - insets.top - insets.bottom);
    return {
        cols,
        rows,
        insets,
        safe: {
            x: safeX,
            y: safeY,
            w: safeW,
            h: safeH,
        },
    };
}
/**
 * Validate that a rect fits within the safe viewport
 *
 * HARD RULE: No widget may have:
 * - x + w > safe.w (right edge exceeds safe width)
 * - y + h > safe.h (bottom edge exceeds safe height)
 *
 * CRITICAL: Must leave 1 cell buffer (maxRight <= safeRight - 1)
 *
 * Returns true if valid, false if violates bounds
 */
export function validateRect(rect, viewport) {
    const errors = [];
    const safeRight = viewport.safe.x + viewport.safe.w;
    const safeBottom = viewport.safe.y + viewport.safe.h;
    // Check X bounds
    if (rect.x < viewport.safe.x) {
        errors.push(`x=${rect.x} < safe.x=${viewport.safe.x}`);
    }
    // Check Y bounds
    if (rect.y < viewport.safe.y) {
        errors.push(`y=${rect.y} < safe.y=${viewport.safe.y}`);
    }
    // CRITICAL: Check right edge - HARD RULE: x + w <= safe.w - 1 (leave 1 cell buffer)
    // Blessed box with border: actual right edge = x + w + 1 (zero-indexed)
    // Must enforce: x + w <= safe.w - 1 (so rightEdge = x + w + 1 <= safe.w)
    const rightEdge = rect.x + rect.w;
    if (rightEdge > viewport.safe.w - 1) {
        errors.push(`rightEdge=${rightEdge} > safeW-1=${viewport.safe.w - 1} (x=${rect.x} w=${rect.w})`);
    }
    // CRITICAL: Check bottom edge - HARD RULE: y + h <= safe.h - 1 (leave 1 cell buffer)
    // Blessed box with border: actual bottom edge = y + h + 1 (zero-indexed)
    // Must enforce: y + h <= safe.h - 1 (so bottomEdge = y + h + 1 <= safe.h)
    const bottomEdge = rect.y + rect.h;
    if (bottomEdge > viewport.safe.h - 1) {
        errors.push(`bottomEdge=${bottomEdge} > safeH-1=${viewport.safe.h - 1} (y=${rect.y} h=${rect.h})`);
    }
    // CRITICAL: Never allow w=0 or h=0
    if (rect.w <= 0) {
        errors.push(`w=${rect.w} <= 0 (invalid)`);
    }
    if (rect.h <= 0) {
        errors.push(`h=${rect.h} <= 0 (invalid)`);
    }
    return {
        valid: errors.length === 0,
        errors,
    };
}
//# sourceMappingURL=safeViewport.js.map