import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { Box, Text } from 'ink';
import { Colors } from '../colors.js';
import { PrepareLabel } from './PrepareLabel.js';
import { isSlashCommand } from '../utils/commandUtils.js';
export const MAX_SUGGESTIONS_TO_SHOW = 8;
export function SuggestionsDisplay({ suggestions, activeIndex, isLoading, width, scrollOffset, userInput, }) {
    // If we're not showing suggestions at all, return null
    if (!isLoading && suggestions.length === 0) {
        return null;
    }
    // Calculate the visible slice based on scrollOffset
    const startIndex = scrollOffset;
    const endIndex = Math.min(scrollOffset + MAX_SUGGESTIONS_TO_SHOW, suggestions.length);
    const visibleSuggestions = suggestions.slice(startIndex, endIndex);
    const isSlashCommandMode = isSlashCommand(userInput);
    let commandNameWidth = 0;
    if (isSlashCommandMode) {
        const maxLabelLength = visibleSuggestions.length
            ? Math.max(...visibleSuggestions.map((s) => s.label.length))
            : 0;
        const maxAllowedWidth = Math.floor(width * 0.35);
        commandNameWidth = Math.max(15, Math.min(maxLabelLength + 2, maxAllowedWidth));
    }
    // Calculate how many lines we need to reserve
    const hasScrollUp = scrollOffset > 0;
    const hasScrollDown = endIndex < suggestions.length;
    const hasCounter = suggestions.length > MAX_SUGGESTIONS_TO_SHOW;
    // Reserve consistent height
    const actualSuggestionLines = isLoading ? 1 : visibleSuggestions.length;
    const linesToFill = MAX_SUGGESTIONS_TO_SHOW - actualSuggestionLines;
    return (_jsxs(Box, { flexDirection: "column", paddingX: 1, width: width, minHeight: MAX_SUGGESTIONS_TO_SHOW + 2, children: [hasScrollUp ? _jsx(Text, { color: Colors.Foreground, children: "\u25B2" }) : _jsx(Text, { children: " " }), isLoading ? (_jsxs(_Fragment, { children: [_jsx(Text, { color: Colors.Gray, children: "Loading suggestions..." }), Array.from({ length: MAX_SUGGESTIONS_TO_SHOW - 1 }).map((_, i) => (_jsx(Text, { children: " " }, `fill-loading-${i}`)))] })) : (_jsxs(_Fragment, { children: [visibleSuggestions.map((suggestion, index) => {
                        const originalIndex = startIndex + index;
                        const isActive = originalIndex === activeIndex;
                        const textColor = isActive ? '#00ff00' : Colors.Foreground;
                        const labelElement = (_jsx(PrepareLabel, { label: suggestion.label, matchedIndex: suggestion.matchedIndex, userInput: userInput, textColor: textColor }));
                        return (_jsx(Box, { width: width, children: _jsx(Box, { flexDirection: "row", children: isSlashCommandMode ? (_jsxs(_Fragment, { children: [_jsx(Box, { width: commandNameWidth, flexShrink: 0, children: labelElement }), suggestion.description ? (_jsx(Box, { flexGrow: 1, marginLeft: 1, children: _jsx(Text, { color: textColor, wrap: "wrap", children: suggestion.description }) })) : null] })) : (_jsxs(_Fragment, { children: [labelElement, suggestion.description ? (_jsx(Box, { flexGrow: 1, marginLeft: 1, children: _jsx(Text, { color: textColor, wrap: "wrap", children: suggestion.description }) })) : null] })) }) }, `${suggestion.value}-${originalIndex}`));
                    }), Array.from({ length: linesToFill }).map((_, i) => (_jsx(Text, { children: " " }, `fill-${i}`)))] })), hasScrollDown || hasCounter ? (_jsxs(Box, { flexDirection: "row", justifyContent: "space-between", children: [_jsx(Text, { color: Colors.Gray, children: hasScrollDown ? '▼' : ' ' }), hasCounter && (_jsxs(Text, { color: Colors.Gray, children: ["(", activeIndex + 1, "/", suggestions.length, ")"] }))] })) : (_jsx(Text, { children: " " }))] }));
}
//# sourceMappingURL=SuggestionsDisplay.js.map