/**
 * SuggestionList – container for text suggestions (primitive)
 *
 * Renders list of SuggestionItem from context; uses activeIndex for highlight.
 * Optional renderItem. When not using renderItem, enableHighlightMarkup and
 * highlightMarkupOptions control parsing of <mark>...</mark> in suggestion text.
 */
import React from 'react';
import { useSuggestionsContext } from './SuggestionsContext';
import { SuggestionItem } from './SuggestionItem';
import { clsx } from 'clsx';
const listStyle = {
    margin: 0,
    padding: '4px 0',
};
export function SuggestionList({ maxItems = 10, className, style, listClassName, enableHighlightMarkup = true, highlightMarkupOptions, renderItem, }) {
    const { suggestions, activeIndex, loading, selectSuggestion, getAllNavigableItems, } = useSuggestionsContext();
    const items = suggestions.slice(0, maxItems);
    const navigableItems = getAllNavigableItems();
    const suggestionStartIndex = navigableItems.findIndex((n) => n.type === 'suggestion');
    const activeIsInSuggestions = suggestionStartIndex >= 0 && activeIndex >= suggestionStartIndex && activeIndex < suggestionStartIndex + items.length;
    // When loading with previous results, show previous results (no loading UI)
    if (items.length === 0)
        return null;
    return (React.createElement("div", { className: clsx('seekora-suggestions-list', className), style: style },
        React.createElement("ul", { className: clsx('seekora-suggestions-list-ul', listClassName), style: listStyle, role: "listbox" }, items.map((suggestion, i) => {
            const globalIndex = suggestionStartIndex >= 0 ? suggestionStartIndex + i : i;
            const isActive = activeIsInSuggestions && activeIndex === globalIndex;
            const onSelect = () => selectSuggestion(suggestion, globalIndex);
            if (renderItem) {
                return (React.createElement("li", { key: suggestion.objectID ?? suggestion.query ?? i, role: "option" }, renderItem(suggestion, globalIndex, isActive, onSelect)));
            }
            return (React.createElement(SuggestionItem, { key: suggestion.objectID ?? suggestion.query ?? i, suggestion: suggestion, index: globalIndex, isActive: isActive, onSelect: onSelect, enableHighlightMarkup: enableHighlightMarkup, highlightMarkupOptions: highlightMarkupOptions }));
        }))));
}
