import React, { useMemo } from "react";
import { Box, Text } from "ink";
import { getSlashCommandManager } from "../../commands/slash-commands.js";
export const MAX_SUGGESTIONS = 15;
export const VISIBLE_SUGGESTIONS = 10;
/**
 * Get argument suggestions for a command
 */
function getArgumentSuggestions(command, currentArgs) {
    if (!command.arguments || command.arguments.length === 0) {
        return [];
    }
    const suggestions = [];
    const lowerArgs = currentArgs.toLowerCase().trim();
    for (const arg of command.arguments) {
        // Parse possible values from description
        // e.g., "on, off, status" or "quick (skip expensive), full (all tests)"
        const desc = arg.description;
        // Extract values from patterns like "value1, value2, value3" or "value1 (desc), value2 (desc)"
        const valueMatches = desc.match(/(?:^|,\s*)([a-z0-9_-]+)(?:\s*\([^)]*\))?/gi);
        if (valueMatches) {
            for (const match of valueMatches) {
                const value = match.replace(/^,\s*/, '').replace(/\s*\([^)]*\)$/, '').trim();
                if (value && value.length > 0 && !value.includes(' ')) {
                    // Filter by current input
                    if (!lowerArgs || value.toLowerCase().startsWith(lowerArgs)) {
                        suggestions.push({
                            command: value,
                            description: `${arg.name}: ${desc}`,
                            isArgument: true,
                        });
                    }
                }
            }
        }
    }
    return suggestions;
}
export function filterCommandSuggestions(suggestions, input) {
    const lowerInput = input.toLowerCase();
    // Check if user is typing arguments for a command (e.g., "/ai-test qu")
    const parts = input.trim().split(/\s+/);
    if (parts.length >= 1 && parts[0].startsWith('/')) {
        const cmdName = parts[0].slice(1).toLowerCase();
        const slashManager = getSlashCommandManager();
        const command = slashManager.getCommand(cmdName);
        // If command exists and user has typed space after it, show argument suggestions
        if (command && (parts.length > 1 || input.endsWith(' '))) {
            const currentArg = parts.length > 1 ? parts.slice(1).join(' ') : '';
            const argSuggestions = getArgumentSuggestions(command, currentArg);
            if (argSuggestions.length > 0) {
                return argSuggestions.slice(0, MAX_SUGGESTIONS);
            }
        }
    }
    // If just "/" is typed, show most useful commands first
    if (lowerInput === "/") {
        const priorityCommands = [
            '/help', '/model', '/clear', '/ai-test', '/cost',
            '/theme', '/memory', '/context', '/export', '/yolo',
            '/checkpoints', '/restore', '/commit', '/review', '/security'
        ];
        const sorted = [...suggestions].sort((a, b) => {
            const aIndex = priorityCommands.indexOf(a.command.toLowerCase());
            const bIndex = priorityCommands.indexOf(b.command.toLowerCase());
            if (aIndex === -1 && bIndex === -1)
                return 0;
            if (aIndex === -1)
                return 1;
            if (bIndex === -1)
                return -1;
            return aIndex - bIndex;
        });
        return sorted.slice(0, MAX_SUGGESTIONS);
    }
    return suggestions
        .filter((s) => s.command.toLowerCase().startsWith(lowerInput))
        .slice(0, MAX_SUGGESTIONS);
}
// Memoized individual suggestion item
const SuggestionItem = React.memo(function SuggestionItem({ command, description, isSelected, isArgument, }) {
    return (React.createElement(Box, { paddingLeft: 1 },
        isArgument && React.createElement(Text, { color: "gray" }, "  \u2192 "),
        React.createElement(Text, { color: isSelected ? "black" : isArgument ? "yellow" : "white", backgroundColor: isSelected ? "cyan" : undefined }, command),
        React.createElement(Box, { marginLeft: 1 },
            React.createElement(Text, { color: "gray" }, description))));
});
export const CommandSuggestions = React.memo(function CommandSuggestions({ suggestions, input, selectedIndex, isVisible, }) {
    // Memoize filtered suggestions
    const filteredSuggestions = useMemo(() => filterCommandSuggestions(suggestions, input), [suggestions, input]);
    // Calculate visible window for scrolling
    const visibleWindow = useMemo(() => {
        const total = filteredSuggestions.length;
        if (total <= VISIBLE_SUGGESTIONS) {
            return { start: 0, end: total };
        }
        // Center the selected item in the visible window
        let start = Math.max(0, selectedIndex - Math.floor(VISIBLE_SUGGESTIONS / 2));
        let end = start + VISIBLE_SUGGESTIONS;
        if (end > total) {
            end = total;
            start = Math.max(0, end - VISIBLE_SUGGESTIONS);
        }
        return { start, end };
    }, [filteredSuggestions.length, selectedIndex]);
    // Early return for invisible state
    if (!isVisible)
        return null;
    const visibleSuggestions = filteredSuggestions.slice(visibleWindow.start, visibleWindow.end);
    const totalMatches = filteredSuggestions.length;
    const totalCommands = suggestions.length;
    const hasMore = visibleWindow.end < totalMatches;
    const hasLess = visibleWindow.start > 0;
    return (React.createElement(Box, { marginTop: 1, flexDirection: "column" },
        hasLess && (React.createElement(Box, { paddingLeft: 1 },
            React.createElement(Text, { color: "gray", dimColor: true },
                "\u2191 ",
                visibleWindow.start,
                " more above..."))),
        visibleSuggestions.map((suggestion, index) => (React.createElement(SuggestionItem, { key: suggestion.command, command: suggestion.command, description: suggestion.description, isSelected: index + visibleWindow.start === selectedIndex, isArgument: suggestion.isArgument }))),
        hasMore && (React.createElement(Box, { paddingLeft: 1 },
            React.createElement(Text, { color: "gray", dimColor: true },
                "\u2193 ",
                totalMatches - visibleWindow.end,
                " more below..."))),
        React.createElement(Box, { marginTop: 1 },
            React.createElement(Text, { color: "gray", dimColor: true },
                "\u2191\u2193 navigate \u2022 Enter/Tab select \u2022 Esc cancel \u2022 ",
                totalMatches,
                "/",
                totalCommands,
                " commands"))));
});
//# sourceMappingURL=CommandSuggestions.js.map