import { getKeybindings, truncateToWidth, visibleWidth, wrapTextWithAnsi, } from "@earendil-works/pi-tui";
import { theme } from "../theme/theme.js";
import { formatKeyText } from "./keybinding-hints.js";
/** Component that displays the current search query */
export class SearchLine {
    constructor(treeList) {
        this.treeList = treeList;
    }
    invalidate() { }
    render(width) {
        const query = this.treeList.getSearchQuery();
        if (query) {
            return [truncateToWidth(`  ${theme.fg("muted", "Type to search:")} ${theme.fg("accent", query)}`, width)];
        }
        return [truncateToWidth(`  ${theme.fg("muted", "Type to search:")}`, width)];
    }
    handleInput(_keyData) { }
}
/** Component that renders tree help as semantic rows with chunk-aware wrapping */
export class TreeHelp {
    invalidate() { }
    render(width) {
        const items = TREE_HELP_ITEMS.map(({ keys, label, labelFirst }) => {
            const text = formatHelpKeys(keys);
            if (!text)
                return label;
            return labelFirst ? `${label} ${text}` : `${text} ${label}`;
        });
        const availableWidth = Math.max(1, width);
        const indent = "  ";
        const separator = " · ";
        const lines = [];
        let currentLine = "";
        for (const item of items) {
            const candidate = currentLine
                ? `${currentLine}${separator}${item}`
                : visibleWidth(`${indent}${item}`) <= availableWidth
                    ? `${indent}${item}`
                    : item;
            if (!currentLine || visibleWidth(candidate) <= availableWidth) {
                currentLine = candidate;
                continue;
            }
            lines.push(...wrapTextWithAnsi(currentLine.trimEnd(), availableWidth));
            currentLine = visibleWidth(`${indent}${item}`) <= availableWidth ? `${indent}${item}` : item;
        }
        if (currentLine) {
            lines.push(...wrapTextWithAnsi(currentLine.trimEnd(), availableWidth));
        }
        return lines.map((line) => theme.fg("muted", line));
    }
}
const TREE_HELP_ITEMS = [
    { keys: ["tui.select.up", "tui.select.down"], label: "move" },
    { keys: ["tui.editor.cursorLeft", "tui.editor.cursorRight"], label: "page" },
    { keys: ["app.tree.foldOrUp", "app.tree.unfoldOrDown"], label: "branch" },
    { keys: ["app.tree.editLabel"], label: "label" },
    { keys: ["app.tree.toggleLabelTimestamp"], label: "label time" },
    {
        keys: [
            "app.tree.filter.default",
            "app.tree.filter.noTools",
            "app.tree.filter.userOnly",
            "app.tree.filter.labeledOnly",
            "app.tree.filter.all",
        ],
        label: "filters",
        labelFirst: true,
    },
    { keys: ["app.tree.filter.cycleForward", "app.tree.filter.cycleBackward"], label: "cycle", labelFirst: true },
];
function formatHelpKeys(keybindings) {
    const keys = [];
    for (const keybinding of keybindings) {
        const key = getKeybindings().getKeys(keybinding)[0];
        if (key !== undefined)
            keys.push(key);
    }
    if (keys.length === 0)
        return "";
    return formatKeyText(compactRawKeys(keys))
        .replace(/\bpageup\b/g, "pgup")
        .replace(/\bpagedown\b/g, "pgdn")
        .replace(/\bup\b/g, "↑")
        .replace(/\bdown\b/g, "↓")
        .replace(/\bleft\b/g, "←")
        .replace(/\bright\b/g, "→");
}
function compactRawKeys(keys) {
    if (keys.length === 1)
        return keys[0];
    const parts = keys.map((key) => {
        const separatorIndex = key.lastIndexOf("+");
        return separatorIndex === -1
            ? { prefix: "", suffix: key }
            : { prefix: key.slice(0, separatorIndex + 1), suffix: key.slice(separatorIndex + 1) };
    });
    const prefix = parts[0].prefix;
    return prefix && parts.every((part) => part.prefix === prefix)
        ? `${prefix}${parts.map((part) => part.suffix).join("/")}`
        : keys.join("/");
}
//# sourceMappingURL=tree-selector-help.js.map