'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var jsxRuntime = require('react/jsx-runtime');
var react = require('@chakra-ui/react');
var chakraReactSelect = require('chakra-react-select');
var React = require('react');
var DropdownIndicator = require('./buildingBlocks/DropdownIndicator.cjs');
var LoadingIndicator = require('./buildingBlocks/LoadingIndicator.cjs');
var LoadingMessage = require('./buildingBlocks/LoadingMessage.cjs');
var NoOptionsMessage = require('./buildingBlocks/NoOptionsMessage.cjs');
var Option = require('./buildingBlocks/Option.cjs');

var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source)
    if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
      target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(source)) {
      if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
        target[prop] = source[prop];
    }
  return target;
};
function isSingleValue(option) {
  return (option == null ? void 0 : option.value) !== void 0;
}
function isMultiValue(option) {
  return Array.isArray(option);
}
function isSingleSelectOptions(options) {
  return Array.isArray(options) && options.length > 0 && options[0].value !== void 0;
}
function isGroupSelectOptions(options) {
  return Array.isArray(options) && options.length > 0 && options[0].options !== void 0;
}
const Select = function(_a) {
  var _b = _a, {
    name,
    options,
    isMulti,
    max,
    hideSelectedOptions,
    classNames,
    chakraStyles,
    innerRef,
    defaultValue,
    value,
    formatGroupLabel,
    selectedOptionColorScheme,
    selectedOptionStyle,
    onMenuOpen,
    onMenuClose,
    closeMenuOnSelect,
    components,
    creatable = false
  } = _b, otherProps = __objRest(_b, [
    "name",
    "options",
    "isMulti",
    "max",
    "hideSelectedOptions",
    "classNames",
    "chakraStyles",
    "innerRef",
    "defaultValue",
    "value",
    "formatGroupLabel",
    "selectedOptionColorScheme",
    "selectedOptionStyle",
    "onMenuOpen",
    "onMenuClose",
    "closeMenuOnSelect",
    "components",
    "creatable"
  ]);
  const [menuIsOpen, setMenuIsOpen] = React.useState(false);
  const [limitIsReached, setLimitIsReached] = React.useState(false);
  const getValue = (someValue) => {
    var _a2;
    if (someValue) {
      if (isMultiValue(someValue)) {
        return someValue.map((multiValue) => {
          var _a3;
          return __spreadProps(__spreadValues({}, multiValue), {
            label: (_a3 = multiValue.label) != null ? _a3 : String(multiValue.value)
          });
        });
      } else if (isSingleValue(someValue)) {
        return __spreadProps(__spreadValues({}, someValue), {
          label: (_a2 = someValue.label) != null ? _a2 : String(someValue.value)
        });
      }
    }
    return void 0;
  };
  const getSelectOptions = (someOptions) => {
    if (isGroupSelectOptions(someOptions)) {
      return someOptions.map(({ label, options: groupedOptions }) => ({
        label,
        options: groupedOptions.map((groupedOption) => {
          var _a2;
          return __spreadProps(__spreadValues({}, groupedOption), {
            label: (_a2 = groupedOption.label) != null ? _a2 : String(groupedOption.value)
          });
        })
      }));
    } else if (isSingleSelectOptions(someOptions)) {
      return someOptions.map((singleOption) => {
        var _a2;
        return __spreadProps(__spreadValues({}, singleOption), {
          label: (_a2 = singleOption.label) != null ? _a2 : String(singleOption.value)
        });
      });
    }
    return [];
  };
  const ChakraReactElement = creatable ? chakraReactSelect.CreatableSelect : chakraReactSelect.Select;
  return /* @__PURE__ */ jsxRuntime.jsx(
    ChakraReactElement,
    __spreadValues({
      defaultValue: getValue(defaultValue),
      value: getValue(value),
      options: getSelectOptions(options),
      ref: innerRef,
      isMulti,
      "data-testid": `${name}_field`,
      loadingMessage: void 0,
      noOptionsMessage: void 0,
      chakraStyles,
      components: __spreadValues({
        DropdownIndicator,
        IndicatorSeparator: void 0,
        LoadingIndicator,
        LoadingMessage,
        NoOptionsMessage,
        Option: Option["default"]
      }, components),
      onChange: otherProps.onChange ? otherProps.onChange : (newValue, actionMeta) => {
        var _a2;
        if (isMulti && max) {
          if (newValue.length >= max) {
            setLimitIsReached(true);
            setMenuIsOpen(false);
          } else {
            setLimitIsReached(false);
          }
        }
        (_a2 = otherProps.onChange) == null ? void 0 : _a2.call(otherProps, newValue, actionMeta);
      },
      formatGroupLabel: formatGroupLabel ? formatGroupLabel : (group) => /* @__PURE__ */ jsxRuntime.jsx(react.Text, { children: 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 : true),
      hideSelectedOptions: hideSelectedOptions || false
    }, otherProps)
  );
};

exports["default"] = Select;
exports.isGroupSelectOptions = isGroupSelectOptions;
exports.isMultiValue = isMultiValue;
exports.isSingleSelectOptions = isSingleSelectOptions;
exports.isSingleValue = isSingleValue;
//# sourceMappingURL=Select.cjs.map
