"use client";
import { jsx } from 'react/jsx-runtime';
import { ComboboxProvider } from '../../node_modules/.pnpm/@ariakit_react-core@0.4.16_react-dom@19.1.1_react@19.1.1__react@19.1.1/node_modules/@ariakit/react-core/esm/combobox/combobox-provider.js';
import { createContext, useContext, useState, useId, useRef, useCallback, useMemo } from 'react';
import { Root as Root2 } from '../../node_modules/.pnpm/@radix-ui_react-select@2.2.5_@types_react-dom@19.1.7_@types_react@19.1.9__@types_react@19.1.9_irkq3yh3qlkawac7zwq2rwd6bi/node_modules/@radix-ui/react-select/dist/index.js';

/*
Root context to manage the Select control
@remarks Only for internal usage.
*/
const SelectContext = createContext(undefined);
const useSelectContext = () => {
    const context = 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] = useState(defaultValue);
    const [internalSearchValue, setInternalSearchValue] = useState(defaultSearchValue);
    const [internalOpen, setInternalOpen] = useState(defaultOpen);
    const [items, setItems] = useState({});
    const id = useId();
    const isSelectionInProgress = useRef(false);
    const computedValue = providedValue ?? internalValue;
    const searchValue = providedSearchValue ?? internalSearchValue;
    const open = providedOpen ?? internalOpen;
    const updateSelectionInProgress = useCallback((value) => {
        isSelectionInProgress.current = value;
    }, []);
    const setValue = 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 = useCallback((value) => {
        setInternalSearchValue(value);
        onSearch?.(value);
    }, [onSearch]);
    const handleOpenChange = useCallback((value) => {
        if (isSelectionInProgress.current)
            return;
        setInternalOpen(value);
        onOpenChange?.(value);
    }, [onOpenChange]);
    const registerItem = useCallback(item => {
        setItems(prev => ({ ...prev, [item.value]: item }));
    }, []);
    const unregisterItem = 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 = useMemo(() => {
        if (!computedValue)
            return '';
        if (typeof computedValue === 'string')
            return computedValue;
        if (Array.isArray(computedValue) && computedValue.length)
            return `${SELECT_INTERNAL_VALUE}-${id}`;
        return String(computedValue) ?? '';
    }, [computedValue, id]);
    const element = (jsx(ComboboxProvider, { resetValueOnHide: true, focusLoop: false, includesBaseElement: false, value: searchValue, setValue: setSearchValue, open: open, setOpen: handleOpenChange, children: children }));
    return (jsx(SelectContext.Provider, { value: {
            value: computedValue,
            registerItem,
            unregisterItem,
            autocomplete,
            autocompleteMode,
            searchValue,
            multiple,
            items,
            updateSelectionInProgress,
            setValue
        }, children: jsx(Root2, { autoComplete: htmlAutoComplete, value: radixValue, onValueChange: setValue, open: open, onOpenChange: handleOpenChange, ...rest, children: autocomplete ? element : children }) }));
};

export { SelectRoot, useSelectContext };
//# sourceMappingURL=select-root.js.map
