/**
 * Typeahead utilities for dropdown components
 * Provides functions to search and navigate through options by typing
 */
/**
 * Finds the next matching option based on typeahead input
 * Supports cycling through multiple matches and multi-character search
 */
function findNextMatchingOption({ options, searchChar, currentIndex, previousSearch = '', }) {
    if (!searchChar || options.length === 0) {
        return -1;
    }
    // Build the search string (either new character or continuation)
    const searchString = searchChar.toLowerCase();
    const isNewSearch = previousSearch === '' || !searchString.startsWith(previousSearch.toLowerCase());
    // For single character searches, we want to cycle through matches
    // For multi-character, we want to find the best match
    const searchTerm = isNewSearch ? searchString : searchString;
    // Find all matching options (not disabled)
    const matchingIndices = [];
    options.forEach((option, index) => {
        if (!option.disabled && option.label.toLowerCase().startsWith(searchTerm)) {
            matchingIndices.push(index);
        }
    });
    if (matchingIndices.length === 0) {
        return -1;
    }
    // If it's a single character and we're repeating the same character,
    // cycle to the next match after current position
    if (searchString.length === 1 && searchString === previousSearch.toLowerCase()) {
        const currentMatchIndex = matchingIndices.findIndex(index => index > currentIndex);
        return currentMatchIndex >= 0
            ? matchingIndices[currentMatchIndex]
            : matchingIndices[0]; // Wrap to first match
    }
    // For new searches or multi-character, find the first match after current position
    // or wrap to the first match if none found after current
    const nextMatch = matchingIndices.find(index => index > currentIndex);
    return nextMatch !== undefined ? nextMatch : matchingIndices[0];
}
const TYPEAHEAD_CHAR_REGEX = /[a-zA-Z0-9\s]/;
/**
 * Checks if a character is valid for typeahead search
 * Excludes control keys and special navigation keys
 */
function isValidTypeaheadChar(key) {
    // Allow letters, numbers, and some symbols
    if (key.length === 1) {
        return TYPEAHEAD_CHAR_REGEX.test(key);
    }
    return false;
}
/**
 * Debounce timeout for resetting typeahead search
 * After this timeout, the next character starts a new search
 */
const TYPEAHEAD_TIMEOUT_MS = 1000;

export { TYPEAHEAD_TIMEOUT_MS, findNextMatchingOption, isValidTypeaheadChar };
