import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { getHistorySearchItems, } from "../prompt/historySearch.js";
import { FuzzyPicker } from "./design/FuzzyPicker.js";
import { flattenCellText, padRightCells, truncateCells } from "./design/textLayout.js";
export function HistorySearchPanel(props) {
    const items = getHistorySearchItems(props.entries, props.query, 9);
    return (_jsx(FuzzyPicker, { title: "Search Prompts", query: props.query, placeholder: "Filter history", items: items, selectedIndex: props.selectedIndex, width: props.width, emptyMessage: props.entries.length === 0 ? "No prompt history yet" : "No matching prompts", footer: "Enter use  Esc close  Up/Down select  Ctrl+U clear", tone: "brand", getKey: (item) => item.id, renderItem: renderHistoryRow, renderPreview: renderHistoryPreview }));
}
function renderHistoryRow(item, selected) {
    return (_jsxs(Text, { color: selected ? "white" : "gray", children: [_jsx(Text, { color: selected ? "cyan" : "gray", children: padRightCells(item.ageLabel, 8) }), truncateCells(flattenCellText(item.firstLine), 84)] }));
}
function renderHistoryPreview(item) {
    const lines = item.text.split(/\r?\n/);
    const visible = lines.slice(0, 6);
    const more = Math.max(0, lines.length - visible.length);
    return (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Text, { color: "gray", children: ["match ", _jsx(Text, { color: "white", children: item.match }), "  ", "lines ", _jsx(Text, { color: "white", children: String(item.lineCount) })] }), visible.map((line, index) => (_jsx(Text, { color: "gray", children: truncateCells(flattenCellText(line), 96) }, `${index}:${line}`))), more > 0 && _jsx(Text, { color: "gray", children: `... +${more} more lines` })] }));
}
