/**
 * Terminal Dimensions Hook
 *
 * Provides reactive terminal dimensions that update on resize.
 */
import { useSyncExternalStore } from "react";
// ============================================================================
// Constants
// ============================================================================
/** Default terminal dimensions (fallback) */
const DEFAULT_ROWS = 24;
const DEFAULT_COLUMNS = 80;
/** Minimum dimensions for layout calculations */
const MIN_ROWS = 10;
const MIN_COLUMNS = 40;
/**
 * Create a singleton terminal dimensions store.
 * Properly encapsulates state and manages subscriptions.
 */
const createTerminalStore = () => {
    let dimensions = {
        rows: Math.max(MIN_ROWS, process.stdout.rows ?? DEFAULT_ROWS),
        columns: Math.max(MIN_COLUMNS, process.stdout.columns ?? DEFAULT_COLUMNS)
    };
    const listeners = new Set();
    const update = () => {
        const rows = Math.max(MIN_ROWS, process.stdout.rows ?? DEFAULT_ROWS);
        const columns = Math.max(MIN_COLUMNS, process.stdout.columns ?? DEFAULT_COLUMNS);
        if (rows !== dimensions.rows || columns !== dimensions.columns) {
            dimensions = { rows, columns };
            listeners.forEach((listener) => listener());
        }
    };
    // Listen to resize events at module level
    process.stdout.on("resize", update);
    return {
        getSnapshot: () => dimensions,
        subscribe: (listener) => {
            listeners.add(listener);
            // Check for dimension changes on subscribe
            update();
            return () => {
                listeners.delete(listener);
            };
        }
    };
};
/** Singleton store instance */
const terminalStore = createTerminalStore();
// ============================================================================
// Hook
// ============================================================================
/**
 * Get reactive terminal dimensions.
 *
 * Returns current terminal rows and columns, updating on resize.
 * Uses useSyncExternalStore for immediate synchronization with stdout.
 */
export const useTerminalDimensions = () => useSyncExternalStore(terminalStore.subscribe, terminalStore.getSnapshot, terminalStore.getSnapshot);
//# sourceMappingURL=useTerminalDimensions.js.map