import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { flattenCellText, padRightCells, truncateCells, } from "./design/textLayout.js";
export function CommandSuggestions(props) {
    if (props.suggestions.length === 0)
        return null;
    const nameWidth = props.width >= 96 ? 28 : 22;
    const descriptionWidth = Math.max(20, props.width - nameWidth - 8);
    const rows = visibleSuggestionRows(props.suggestions, props.selectedIndex, props.width >= 72 ? 5 : 4);
    return (_jsxs(Box, { flexDirection: "column", paddingX: 1, paddingBottom: 1, children: [rows.map(({ suggestion, index }) => {
                const selected = index === props.selectedIndex;
                return (_jsxs(Box, { children: [_jsx(Text, { color: selected ? "cyan" : "gray", inverse: selected, children: selected ? ">" : " " }), _jsx(Text, { color: selected ? "cyan" : "gray", children: ` ${padRightCells(formatName(suggestion), nameWidth)}` }), _jsx(Text, { color: selected ? "white" : "gray", children: truncateCells(flattenCellText(suggestion.description), descriptionWidth) })] }, suggestion.id));
            }), _jsx(Text, { color: "gray", children: "  Tab complete  Up/Down select" })] }));
}
export function visibleSuggestionRows(suggestions, selectedIndex, maxRows) {
    const limit = Math.max(1, maxRows);
    if (suggestions.length <= limit) {
        return suggestions.map((suggestion, index) => ({ suggestion, index }));
    }
    const safeSelected = Math.max(0, Math.min(suggestions.length - 1, selectedIndex));
    const start = Math.max(0, Math.min(safeSelected - Math.floor(limit / 2), suggestions.length - limit));
    return suggestions
        .slice(start, start + limit)
        .map((suggestion, offset) => ({ suggestion, index: start + offset }));
}
function formatName(suggestion) {
    const usage = suggestion.usage ? ` ${suggestion.usage}` : "";
    return `/${suggestion.name}${usage}`;
}
export { padRightCells, truncateCells } from "./design/textLayout.js";
