'use strict';

var jsxRuntime = require('../../node_modules/.pnpm/react@18.2.0/node_modules/react/jsx-runtime.cjs');
var comboboxProvider = require('../../node_modules/.pnpm/@ariakit_react-core@0.4.16_react-dom@18.3.1_react@18.2.0__react@18.2.0/node_modules/@ariakit/react-core/esm/combobox/combobox-provider.cjs');
var React = require('react');
var index = require('../../node_modules/.pnpm/@radix-ui_react-select@2.2.5_@types_react-dom@18.0.11_@types_react@18.2.12_react-dom@18.3.1_react@18.2.0__react@18.2.0/node_modules/@radix-ui/react-select/dist/index.cjs');

/*
Root context to manage the Select control
@remarks Only for internal usage.
*/
const SelectContext = React.createContext(undefined);
const useSelectContext = () => {
    const context = React.useContext(SelectContext);
    if (!context) {
        throw new Error('useSelectContext must be used within a SelectProvider');
    }
    const shouldFilter = !!(context?.autocomplete &&
        context?.autocompleteMode === 'auto' &&
        context?.searchValue?.length);
    return {
        ...context,
        shouldFilter
    };
};
const SELECT_INTERNAL_VALUE = 'SELECT_INTERNAL_VALUE';
const SelectRoot = (props) => {
    const { children, value: providedValue, onValueChange, defaultValue, autocomplete, autocompleteMode = 'auto', searchValue: providedSearchValue, onSearch, defaultSearchValue = '', open: providedOpen, defaultOpen = false, onOpenChange, htmlAutoComplete, multiple = false, ...rest } = props;
    const [internalValue, setInternalValue] = React.useState(defaultValue);
    const [internalSearchValue, setInternalSearchValue] = React.useState(defaultSearchValue);
    const [internalOpen, setInternalOpen] = React.useState(defaultOpen);
    const [items, setItems] = React.useState({});
    const id = React.useId();
    const isSelectionInProgress = React.useRef(false);
    const computedValue = providedValue ?? internalValue;
    const searchValue = providedSearchValue ?? internalSearchValue;
    const open = providedOpen ?? internalOpen;
    const updateSelectionInProgress = React.useCallback((value) => {
        isSelectionInProgress.current = value;
    }, []);
    const setValue = React.useCallback((value) => {
        /*
         * If the select is placed inside a form, onChange is called with an empty value
         * WORKAROUND FOR ISSUE https://github.com/radix-ui/primitives/issues/3135
         */
        if (value === '')
            return;
        if (multiple) {
            updateSelectionInProgress(true);
            const set = new Set(Array.isArray(computedValue)
                ? computedValue
                : [computedValue ?? ''].filter(Boolean));
            if (set.has(value))
                set.delete(value);
            else
                set.add(value);
            const newValue = Array.from(set);
            setInternalValue(newValue);
            onValueChange?.(newValue);
        }
        else {
            setInternalValue(value);
            onValueChange?.(value);
        }
    }, [multiple, onValueChange, computedValue, updateSelectionInProgress]);
    const setSearchValue = React.useCallback((value) => {
        setInternalSearchValue(value);
        onSearch?.(value);
    }, [onSearch]);
    const handleOpenChange = React.useCallback((value) => {
        if (isSelectionInProgress.current)
            return;
        setInternalOpen(value);
        onOpenChange?.(value);
    }, [onOpenChange]);
    const registerItem = React.useCallback(item => {
        setItems(prev => ({ ...prev, [item.value]: item }));
    }, []);
    const unregisterItem = React.useCallback(value => {
        setItems(prev => {
            const { [value]: _, ...rest } = prev;
            return rest;
        });
    }, []);
    /*
     * Radix internally shows the placeholder when the value is empty.
     * This value is used to manage the internal value of Radix Select to make it work
     */
    const radixValue = React.useMemo(() => {
        if (!computedValue)
            return '';
        if (typeof computedValue === 'string')
            return computedValue;
        if (computedValue.length)
            return `${SELECT_INTERNAL_VALUE}-${id}`;
        return '';
    }, [computedValue, id]);
    const element = (jsxRuntime.jsxRuntimeExports.jsx(comboboxProvider.ComboboxProvider, { resetValueOnHide: true, focusLoop: false, includesBaseElement: false, value: searchValue, setValue: setSearchValue, open: open, setOpen: handleOpenChange, children: children }));
    return (jsxRuntime.jsxRuntimeExports.jsx(SelectContext.Provider, { value: {
            value: computedValue,
            registerItem,
            unregisterItem,
            autocomplete,
            autocompleteMode,
            searchValue,
            multiple,
            items,
            updateSelectionInProgress,
            setValue
        }, children: jsxRuntime.jsxRuntimeExports.jsx(index.Root, { autoComplete: htmlAutoComplete, value: radixValue, onValueChange: setValue, open: open, onOpenChange: handleOpenChange, ...rest, children: autocomplete ? element : children }) }));
};

exports.SelectRoot = SelectRoot;
exports.useSelectContext = useSelectContext;
//# sourceMappingURL=select-root.cjs.map
