import { jsx as _jsx } from "react/jsx-runtime";
import { Box, Text } from 'ink';
import { Colors } from '../colors.js';
const DEFAULT_HEIGHT = 24;
export const AnsiOutputText = ({ data, availableTerminalHeight, width, }) => {
    const linesToShow = availableTerminalHeight && availableTerminalHeight > 0
        ? availableTerminalHeight
        : DEFAULT_HEIGHT;
    let cursorLineIndex = -1;
    for (let i = data.length - 1; i >= 0; i -= 1) {
        const line = data[i];
        if (line.some((token) => token.inverse)) {
            cursorLineIndex = i;
            break;
        }
    }
    let lastNonEmpty = -1;
    for (let i = data.length - 1; i >= 0; i -= 1) {
        const line = data[i];
        const lineText = line.map((token) => token.text).join('');
        if (lineText.trim().length > 0) {
            lastNonEmpty = i;
            break;
        }
    }
    const renderEndIndex = Math.max(lastNonEmpty, cursorLineIndex);
    if (data.length === 0) {
        return null;
    }
    const endIndex = renderEndIndex + 1;
    const startIndex = Math.max(0, endIndex - linesToShow);
    const linesToRender = data.slice(startIndex, endIndex);
    return (_jsx(Box, { flexDirection: "column", width: width, flexShrink: 0, children: linesToRender.map((line, lineIndex) => (_jsx(Box, { width: width, flexShrink: 0, children: line.length > 0 ? (line.map((token, tokenIndex) => (_jsx(Text, { color: token.dim ? Colors.DimComment : token.fg || Colors.Foreground, backgroundColor: token.bg || undefined, inverse: token.inverse, bold: token.bold, italic: token.italic, underline: token.underline, children: token.text.replace(/\u00a0/g, ' ') }, tokenIndex)))) : (_jsx(Text, { color: Colors.Foreground, children: " " })) }, lineIndex))) }));
};
//# sourceMappingURL=AnsiOutput.js.map