import * as __WEBPACK_EXTERNAL_MODULE__chakra_ui_react__ from "@chakra-ui/react";
import * as __WEBPACK_EXTERNAL_MODULE_chakra_react_select__ from "chakra-react-select";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__buildingBlocks_DropdownIndicator_mjs__ from "./buildingBlocks/DropdownIndicator.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__buildingBlocks_LoadingIndicator_mjs__ from "./buildingBlocks/LoadingIndicator.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__buildingBlocks_LoadingMessage_mjs__ from "./buildingBlocks/LoadingMessage.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__buildingBlocks_NoOptionsMessage_mjs__ from "./buildingBlocks/NoOptionsMessage.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__buildingBlocks_Option_mjs__ from "./buildingBlocks/Option.mjs";
function isSingleValue(option) {
    return option?.value !== void 0;
}
// MultiValue is read-only, meaning it cannot be mutated
function isMultiValue(option) {
    return Array.isArray(option);
}
function isSingleSelectOptions(options) {
    return Array.isArray(options) && options.length > 0 && void 0 !== options[0].value;
}
function isGroupSelectOptions(options) {
    return Array.isArray(options) && options.length > 0 && void 0 !== options[0].options;
}
const Select = function({ name, options, isMulti, max, hideSelectedOptions, classNames, chakraStyles, innerRef, defaultValue, value, formatGroupLabel, selectedOptionColorScheme, selectedOptionStyle, onMenuOpen, onMenuClose, closeMenuOnSelect, components, creatable = false, ...otherProps }) {
    const [menuIsOpen, setMenuIsOpen] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const [limitIsReached, setLimitIsReached] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    /**
   * This code is responsible for inferring the label usage
   * for a default value in multi and single select mode
   */ const getValue = (someValue)=>{
        if (someValue) {
            if (isMultiValue(someValue)) return someValue.map((multiValue)=>({
                    ...multiValue,
                    label: multiValue.label ?? String(multiValue.value)
                }));
            if (isSingleValue(someValue)) return {
                ...someValue,
                label: someValue.label ?? String(someValue.value)
            };
        }
    };
    /**
   * This code is responsible for inferring the label usage
   * for a group and single select
   */ const getSelectOptions = (someOptions)=>{
        if (isGroupSelectOptions(someOptions)) return someOptions.map(({ label, options: groupedOptions })=>({
                label: label,
                options: groupedOptions.map((groupedOption)=>({
                        ...groupedOption,
                        label: groupedOption.label ?? String(groupedOption.value)
                    }))
            }));
        if (isSingleSelectOptions(someOptions)) return someOptions.map((singleOption)=>({
                ...singleOption,
                label: singleOption.label ?? String(singleOption.value)
            }));
        return [];
    };
    const ChakraReactElement = creatable ? __WEBPACK_EXTERNAL_MODULE_chakra_react_select__.CreatableSelect : __WEBPACK_EXTERNAL_MODULE_chakra_react_select__.Select;
    return /*#__PURE__*/ React.createElement(ChakraReactElement, {
        defaultValue: getValue(defaultValue),
        value: getValue(value),
        options: getSelectOptions(options),
        ref: innerRef,
        isMulti: isMulti,
        "data-testid": `${name}_field`,
        loadingMessage: void 0,
        noOptionsMessage: void 0,
        /**
       * @see https://react-select.com/components#adjusting-the-styling
       */ chakraStyles: chakraStyles,
        /**
       * @see https://react-select.com/components#defining-components
       */ components: {
            DropdownIndicator: __WEBPACK_EXTERNAL_MODULE__buildingBlocks_DropdownIndicator_mjs__["default"],
            IndicatorSeparator: void 0,
            LoadingIndicator: __WEBPACK_EXTERNAL_MODULE__buildingBlocks_LoadingIndicator_mjs__["default"],
            LoadingMessage: __WEBPACK_EXTERNAL_MODULE__buildingBlocks_LoadingMessage_mjs__["default"],
            NoOptionsMessage: __WEBPACK_EXTERNAL_MODULE__buildingBlocks_NoOptionsMessage_mjs__["default"],
            Option: __WEBPACK_EXTERNAL_MODULE__buildingBlocks_Option_mjs__["default"],
            ...components
        },
        onChange: otherProps.onChange ? otherProps.onChange : (newValue, actionMeta)=>{
            // Add logic to support max number of elements auto-closing the select menu
            if (isMulti && max) {
                if (newValue.length >= max) {
                    setLimitIsReached(true);
                    setMenuIsOpen(false);
                } else setLimitIsReached(false);
            }
            otherProps.onChange?.(newValue, actionMeta);
        },
        formatGroupLabel: formatGroupLabel ? formatGroupLabel : (group)=>/*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Text, null, group.label),
        selectedOptionColorScheme: selectedOptionColorScheme || 'brand',
        selectedOptionStyle: selectedOptionStyle || 'check',
        onMenuOpen: onMenuOpen ? onMenuOpen : ()=>setMenuIsOpen(!limitIsReached),
        onMenuClose: onMenuClose ? onMenuClose : ()=>setMenuIsOpen(false),
        menuIsOpen: otherProps.menuIsOpen || menuIsOpen,
        closeMenuOnSelect: closeMenuOnSelect || !isMulti || limitIsReached,
        hideSelectedOptions: hideSelectedOptions || false,
        ...otherProps
    });
};
/* ESM default export */ const Select_rslib_entry_ = Select;
export { Select_rslib_entry_ as default, isGroupSelectOptions, isMultiValue, isSingleSelectOptions, isSingleValue };
