"use client";
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useMemo } from "react";
import { triggerManualChangeEvent } from "../form/utils.js";
import { ListSubheader } from "../list/ListSubheader.js";
import { MenuItem } from "../menu/MenuItem.js";
import { defaultAutocompleteExtractor, defaultAutocompleteFilter, defaultAutocompleteOptionProps } from "./defaults.js";
const noop = ()=>{
// do nothing
};
export function FilterAutocompleteOptions(props) {
    const { query, filter = defaultAutocompleteFilter, options, extractor = defaultAutocompleteExtractor, whitespace = "keep", comboboxRef, disableFilter, onAutocomplete = noop, getOptionProps = defaultAutocompleteOptionProps, clearOnAutocomplete, noOptionsChildren = /*#__PURE__*/ _jsx(ListSubheader, {
        children: "No Options"
    }) } = props;
    const filtered = useMemo(()=>{
        if (disableFilter) {
            return options;
        }
        return filter({
            list: options,
            query,
            extractor,
            whitespace
        });
    }, [
        disableFilter,
        extractor,
        filter,
        options,
        query,
        whitespace
    ]);
    return /*#__PURE__*/ _jsxs(_Fragment, {
        children: [
            !filtered.length && noOptionsChildren,
            filtered.map((option, index)=>{
                const label = extractor(option);
                const optionProps = getOptionProps({
                    index,
                    option
                });
                return /*#__PURE__*/ _jsx(MenuItem, {
                    role: "option",
                    ...optionProps,
                    onClick: (event)=>{
                        optionProps?.onClick?.(event);
                        triggerManualChangeEvent(comboboxRef.current, clearOnAutocomplete ? "" : label);
                        onAutocomplete(option);
                    },
                    children: optionProps?.children ?? label
                }, label);
            })
        ]
    });
}

//# sourceMappingURL=FilterAutocompleteOptions.js.map