import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import React from "react";
import { Box, Text } from "ink";
import { isChineseUi } from "../services/ui/languageService.js";
export function VirtualMessageList(props) {
    const window = virtualMessageWindow(props.entries, props.height, props.width, props.estimateRows, props.scrollOffset);
    return (_jsx(Box, { flexDirection: "column", height: props.height, paddingX: 1, width: props.width, children: window.visible.length === 0 && props.empty ? (props.empty) : (_jsxs(_Fragment, { children: [window.hidden > 0 && (_jsx(Box, { children: _jsx(Text, { color: "gray", children: hiddenBeforeText(window.hidden, props.language) }) })), window.visible.map((entry, index) => (_jsx(React.Fragment, { children: props.renderEntry(entry, index) }, index))), window.hiddenAfter > 0 && (_jsx(Box, { children: _jsx(Text, { color: "gray", children: hiddenAfterText(window.hiddenAfter, props.language) }) }))] })) }));
}
function hiddenBeforeText(count, language) {
    if (isChineseUi(language))
        return `^ ${count} 条更早消息已隐藏`;
    return `^ ${count} earlier message${count === 1 ? "" : "s"} hidden`;
}
function hiddenAfterText(count, language) {
    if (isChineseUi(language))
        return `v ${count} 条更新消息已隐藏`;
    return `v ${count} newer message${count === 1 ? "" : "s"} hidden`;
}
export function virtualMessageWindow(entries, height, width, estimateRows, scrollOffset = 0) {
    const selected = [];
    let rows = 0;
    const budget = Math.max(3, height);
    const safeOffset = Math.max(0, Math.min(entries.length, Math.floor(scrollOffset)));
    const endExclusive = Math.max(0, entries.length - safeOffset);
    for (let index = endExclusive - 1; index >= 0; index -= 1) {
        const entry = entries[index];
        if (entry === undefined)
            continue;
        const nextRows = rows + Math.max(1, estimateRows(entry, width));
        if (selected.length > 0 && nextRows > budget)
            break;
        selected.unshift(entry);
        rows = nextRows;
    }
    return {
        visible: selected,
        hidden: Math.max(0, endExclusive - selected.length),
        hiddenAfter: Math.max(0, entries.length - endExclusive),
        rows,
    };
}
