'use strict';

var jsxRuntime = require('../../node_modules/.pnpm/react@18.2.0/node_modules/react/jsx-runtime.cjs');
var React = require('react');
var index = require('../../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.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");
    }
    return context;
};
const SelectRoot = ({ children, value, onValueChange, defaultValue, ...props }) => {
    const [internalValue, setInternalValue] = React.useState(defaultValue);
    const icons = React.useRef({});
    const computedValue = value ?? internalValue;
    const icon = computedValue && icons.current?.[computedValue];
    const setValue = (_value) => {
        onValueChange?.(_value);
        setInternalValue(_value);
    };
    const registerIcon = React.useCallback((value, icon) => {
        icons.current = { ...icons.current, [value]: icon };
    }, []);
    const unregisterIcon = React.useCallback(value => {
        const { [value]: _, ...rest } = icons.current;
        icons.current = rest;
    }, []);
    return (jsxRuntime.jsxRuntimeExports.jsx(SelectContext.Provider, { value: {
            registerIcon,
            unregisterIcon,
            value: { value: computedValue, icon },
        }, children: jsxRuntime.jsxRuntimeExports.jsx(index.Root, { value: computedValue, onValueChange: setValue, ...props, children: children }) }));
};

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