import { jsx as _jsx } from "react/jsx-runtime";
import React, { useRef, useState, useLayoutEffect, useCallback } from "react";
import { Box, measureElement } from "ink";
/**
 * Height-ratchet: once content grows to N rows the outer Box never
 * shrinks below N, preventing jarring visual jumps when streaming
 * content temporarily shortens (e.g. markdown re-parse flicker).
 *
 * Mirrors claude-code's Ratchet component.
 */
export function Ratchet({ children, lock: _lock = "always" }) {
    const innerRef = useRef(null);
    const maxHeight = useRef(0);
    const [minHeight, setMinHeight] = useState(0);
    const outerRef = useCallback((_el) => {
        // Placeholder for future viewport tracking.
    }, []);
    useLayoutEffect(() => {
        if (!innerRef.current)
            return;
        const { height } = measureElement(innerRef.current);
        const termRows = process.stdout.rows ?? 24;
        // Cap live area to 80% of terminal to preserve scrollback access
        const maxAllowed = Math.floor(termRows * 0.8);
        if (height === 0) {
            // Reset when live area is empty (between turns)
            maxHeight.current = 0;
            setMinHeight(0);
        }
        else if (height > maxHeight.current) {
            maxHeight.current = Math.min(height, maxAllowed);
            setMinHeight(maxHeight.current);
        }
    }, [children]);
    return (_jsx(Box, { minHeight: minHeight, ref: outerRef, children: _jsx(Box, { ref: innerRef, flexDirection: "column", children: children }) }));
}
//# sourceMappingURL=Ratchet.js.map