import { j as jsxRuntimeExports } from '../../node_modules/.pnpm/react@18.2.0/node_modules/react/jsx-runtime.js';
import { createContext, useContext, useState, useRef, useCallback } from 'react';
import { Root as Root2 } from '../../node_modules/.pnpm/@radix-ui_react-select@2.1.1_@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.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");
    }
    return context;
};
const SelectRoot = ({ children, value, onValueChange, defaultValue, ...props }) => {
    const [internalValue, setInternalValue] = useState(defaultValue);
    const icons = useRef({});
    const computedValue = value ?? internalValue;
    const icon = computedValue && icons.current?.[computedValue];
    const setValue = (_value) => {
        onValueChange?.(_value);
        setInternalValue(_value);
    };
    const registerIcon = useCallback((value, icon) => {
        icons.current = { ...icons.current, [value]: icon };
    }, []);
    const unregisterIcon = useCallback(value => {
        const { [value]: _, ...rest } = icons.current;
        icons.current = rest;
    }, []);
    return (jsxRuntimeExports.jsx(SelectContext.Provider, { value: {
            registerIcon,
            unregisterIcon,
            value: { value: computedValue, icon },
        }, children: jsxRuntimeExports.jsx(Root2, { value: computedValue, onValueChange: setValue, ...props, children: children }) }));
};

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