"use strict";
// The require scope
var __webpack_require__ = {};
/************************************************************************/ // webpack/runtime/compat_get_default_export
(()=>{
    // getDefaultExport function for compatibility with non-ESM modules
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
// webpack/runtime/define_property_getters
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
// webpack/runtime/has_own_property
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
// webpack/runtime/make_namespace_object
(()=>{
    // define __esModule on exports
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
/************************************************************************/ var __webpack_exports__ = {};
// ESM COMPAT FLAG
__webpack_require__.r(__webpack_exports__);
// EXPORTS
__webpack_require__.d(__webpack_exports__, {
    isSingleSelectOptions: ()=>/* binding */ isSingleSelectOptions,
    default: ()=>/* binding */ Select_rslib_entry_,
    isGroupSelectOptions: ()=>/* binding */ isGroupSelectOptions,
    isMultiValue: ()=>/* binding */ isMultiValue,
    isSingleValue: ()=>/* binding */ isSingleValue
});
const react_namespaceObject = require("@chakra-ui/react");
const external_chakra_react_select_namespaceObject = require("chakra-react-select");
const external_react_namespaceObject = require("react");
const DropdownIndicator_js_namespaceObject = require("./buildingBlocks/DropdownIndicator.js");
var DropdownIndicator_js_default = /*#__PURE__*/ __webpack_require__.n(DropdownIndicator_js_namespaceObject);
const LoadingIndicator_js_namespaceObject = require("./buildingBlocks/LoadingIndicator.js");
var LoadingIndicator_js_default = /*#__PURE__*/ __webpack_require__.n(LoadingIndicator_js_namespaceObject);
const LoadingMessage_js_namespaceObject = require("./buildingBlocks/LoadingMessage.js");
var LoadingMessage_js_default = /*#__PURE__*/ __webpack_require__.n(LoadingMessage_js_namespaceObject);
const NoOptionsMessage_js_namespaceObject = require("./buildingBlocks/NoOptionsMessage.js");
var NoOptionsMessage_js_default = /*#__PURE__*/ __webpack_require__.n(NoOptionsMessage_js_namespaceObject);
const Option_js_namespaceObject = require("./buildingBlocks/Option.js");
var Option_js_default = /*#__PURE__*/ __webpack_require__.n(Option_js_namespaceObject);
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, external_react_namespaceObject.useState)(false);
    const [limitIsReached, setLimitIsReached] = (0, external_react_namespaceObject.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 ? external_chakra_react_select_namespaceObject.CreatableSelect : external_chakra_react_select_namespaceObject.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: DropdownIndicator_js_default(),
            IndicatorSeparator: void 0,
            LoadingIndicator: LoadingIndicator_js_default(),
            LoadingMessage: LoadingMessage_js_default(),
            NoOptionsMessage: NoOptionsMessage_js_default(),
            Option: Option_js_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(react_namespaceObject.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;
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
