var _excluded = ["children", "className", "disabled", "hasError", "multiple", "name", "onChange", "placeholder", "size", "value", "valueDisplay", "trigger", "width", "fullWidth", "id", "defaultValue"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { forwardRef, useEffect } from 'react';
import { useSelect as useMuiSelect, SelectProvider as MuiSelectProvider } from '@mui/base/useSelect';
import { FadedScroll } from '../..';
import { HiddenSelect } from './HiddenSelect';
import { ListContainer, Listbox, ListboxContainer, Root } from './Select.styles';
import { SelectProvider } from './useSelect';
import { Arrow } from './Arrow';
import { Group } from './Group';
import { Option } from './Option';
import { Trigger } from './Trigger';
import { ValueDisplay } from './ValueDisplay';
import { ValueDisplayMultiSelect } from './ValueDisplayMultiSelect';
var SelectWithForwardRef = forwardRef(function (props, ref) {
  var children = props.children,
    className = props.className,
    disabled = props.disabled,
    _props$hasError = props.hasError,
    hasError = _props$hasError === void 0 ? false : _props$hasError,
    multiple = props.multiple,
    name = props.name,
    onChange = props.onChange,
    placeholder = props.placeholder,
    _props$size = props.size,
    size = _props$size === void 0 ? 'm' : _props$size,
    valueFromProps = props.value,
    valueDisplayFromProps = props.valueDisplay,
    triggerFromProps = props.trigger,
    _props$width = props.width,
    width = _props$width === void 0 ? 50 : _props$width,
    fullWidth = props.fullWidth,
    id = props.id,
    defaultValue = props.defaultValue,
    rest = _objectWithoutProperties(props, _excluded);
  var listboxRef = React.useRef(null);
  var _React$useState = React.useState(false),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    isOpen = _React$useState2[0],
    setIsOpen = _React$useState2[1];
  var _useMuiSelect = useMuiSelect({
      listboxRef: listboxRef,
      disabled: disabled,
      multiple: multiple,
      open: isOpen,
      onChange: onChange,
      onOpenChange: setIsOpen,
      value: valueFromProps,
      defaultValue: defaultValue
    }),
    getButtonProps = _useMuiSelect.getButtonProps,
    getListboxProps = _useMuiSelect.getListboxProps,
    contextValue = _useMuiSelect.contextValue,
    value = _useMuiSelect.value,
    getOptionMetadata = _useMuiSelect.getOptionMetadata,
    options = _useMuiSelect.options;
  var valueWithDefault = value !== null && value !== void 0 ? value : defaultValue;
  useEffect(function () {
    if (isOpen) {
      var _listboxRef$current;
      (_listboxRef$current = listboxRef.current) === null || _listboxRef$current === void 0 || _listboxRef$current.focus();
    }
  }, [isOpen]);
  return React.createElement(SelectProvider, {
    getButtonProps: getButtonProps,
    getOptionMetadata: getOptionMetadata,
    value: valueWithDefault
  }, React.createElement(Root, _extends({
    className: className,
    $width: width,
    $fullWidth: fullWidth,
    id: id
  }, rest), triggerFromProps || React.createElement(Trigger, {
    size: size,
    hasError: hasError,
    ref: ref
  }, valueDisplayFromProps || React.createElement(ValueDisplay, {
    placeholder: placeholder
  }), React.createElement(Arrow, null)), React.createElement(ListContainer, {
    "aria-hidden": !isOpen,
    $hidden: !isOpen
  }, React.createElement(ListboxContainer, null, React.createElement(FadedScroll, {
    maxHeight: 50
  }, React.createElement(Listbox, getListboxProps(), React.createElement(MuiSelectProvider, {
    value: contextValue
  }, children)))))), name && valueWithDefault ? React.createElement(HiddenSelect, {
    name: name,
    multiple: multiple,
    value: valueWithDefault,
    options: options
  }) : null);
});
export var Select = SelectWithForwardRef;
Select.Arrow = Arrow;
Select.Group = Group;
Select.Option = Option;
Select.Trigger = Trigger;
Select.ValueDisplay = ValueDisplay;
Select.ValueDisplayMultiSelect = ValueDisplayMultiSelect;