import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React from "react";
import { Box, Text } from "ink";
import { useTheme } from "../theme/theme.js";
const MAX_SUGGESTIONS_TO_SHOW = 8;
export function SlashCommandMenu({ commands, selectedIndex, width }) {
    const theme = useTheme();
    if (commands.length === 0)
        return null;
    const total = commands.length;
    const idx = Math.min(Math.max(selectedIndex, 0), total - 1);
    const start = total <= MAX_SUGGESTIONS_TO_SHOW
        ? 0
        : Math.max(0, Math.min(idx - Math.floor(MAX_SUGGESTIONS_TO_SHOW / 2), total - MAX_SUGGESTIONS_TO_SHOW));
    const end = Math.min(start + MAX_SUGGESTIONS_TO_SHOW, total);
    const visible = commands.slice(start, end);
    const maxLabelLength = Math.max(...commands.map((cmd) => cmd.name.length));
    const commandColumnWidth = Math.min(maxLabelLength, Math.floor(width * 0.5));
    return (_jsxs(Box, { flexDirection: "column", paddingX: 1, width: width, children: [start > 0 && _jsx(Text, { color: theme.text, children: "\u25B2" }), visible.map((cmd, i) => {
                const actualIndex = start + i;
                const previousSectionTitle = commands[actualIndex - 1]?.sectionTitle;
                const shouldRenderSectionHeader = !!cmd.sectionTitle && cmd.sectionTitle !== previousSectionTitle;
                const isSelected = actualIndex === idx;
                const textColor = isSelected ? theme.commandColor : theme.textDim;
                return (_jsxs(Box, { flexDirection: "column", children: [shouldRenderSectionHeader && (_jsxs(Text, { color: theme.textDim, children: ["-- ", cmd.sectionTitle, " --"] })), _jsxs(Box, { flexDirection: "row", backgroundColor: isSelected ? theme.border : undefined, children: [_jsx(Box, { width: commandColumnWidth, flexShrink: 0, children: _jsx(Text, { color: textColor, children: cmd.name }) }), _jsx(Box, { flexGrow: 1, paddingLeft: 3, children: _jsx(Text, { color: textColor, wrap: "truncate", children: cmd.description.slice(0, 100) }) })] })] }, cmd.name));
            }), end < total && _jsx(Text, { color: theme.textDim, children: "\u25BC" }), total > MAX_SUGGESTIONS_TO_SHOW && (_jsxs(Text, { color: theme.textDim, children: ["(", idx + 1, "/", total, ")"] }))] }));
}
/** Filter commands by partial name/alias fuzzy-ish prefix priority. */
export function filterCommands(commands, filter) {
    if (!filter)
        return commands;
    const lower = filter.toLowerCase();
    return commands
        .filter((cmd) => cmd.name.toLowerCase().includes(lower) ||
        cmd.aliases.some((a) => a.toLowerCase().includes(lower)))
        .sort((a, b) => {
        const aName = a.name.toLowerCase();
        const bName = b.name.toLowerCase();
        const aExact = aName === lower;
        const bExact = bName === lower;
        if (aExact && !bExact)
            return -1;
        if (!aExact && bExact)
            return 1;
        const aPrefix = aName.startsWith(lower);
        const bPrefix = bName.startsWith(lower);
        if (aPrefix && !bPrefix)
            return -1;
        if (!aPrefix && bPrefix)
            return 1;
        return 0;
    });
}
//# sourceMappingURL=SlashCommandMenu.js.map