text: import _extends from "@babel/runtime/helpers/esm/extends";
  import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
  import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
  import _typeof from "@babel/runtime/helpers/esm/typeof";
  import React from 'react';
  import PropTypes from 'prop-types';
  import clsx from 'clsx';
  import capitalize from '../utils/capitalize';
  import { refType } from '@material-ui/utils';
  import Menu from '../Menu/Menu';
  import { isFilled } from '../InputBase/utils';
  import useForkRef from '../utils/useForkRef';
  
  function areEqualValues(a, b) {
    if (_typeof(b) === 'object' && b !== null) {
      return a === b;
    }
  
    return String(a) === String(b);
  }
  
  function isEmpty(display) {
    return display == null || typeof display === 'string' && !display.trim();
  }
  /**
   * @ignore - internal component.
   */
  
  
  var SelectInput = React.forwardRef(function SelectInput(props, ref) {
    var autoFocus = props.autoFocus,
        autoWidth = props.autoWidth,
        children = props.children,
        classes = props.classes,
        className = props.className,
        disabled = props.disabled,
        displayEmpty = props.displayEmpty,
        IconComponent = props.IconComponent,
        inputRefProp = props.inputRef,
        _props$MenuProps = props.MenuProps,
        MenuProps = _props$MenuProps === void 0 ? {} : _props$MenuProps,
        multiple = props.multiple,
        name = props.name,
        onBlur = props.onBlur,
        onChange = props.onChange,
        onClose = props.onClose,
        onFocus = props.onFocus,
        onOpen = props.onOpen,
        openProp = props.open,
        readOnly = props.readOnly,
        renderValue = props.renderValue,
        required = props.required,
        SelectDisplayProps = props.SelectDisplayProps,
        tabIndexProp = props.tabIndex,
        type = props.type,
        value = props.value,
        _props$variant = props.variant,
        variant = _props$variant === void 0 ? 'standard' : _props$variant,
        other = _objectWithoutProperties(props, ["autoFocus", "autoWidth", "children", "classes", "className", "disabled", "displayEmpty", "IconComponent", "inputRef", "MenuProps", "multiple", "name", "onBlur", "onChange", "onClose", "onFocus", "onOpen", "open", "readOnly", "renderValue", "required", "SelectDisplayProps", "tabIndex", "type", "value", "variant"]);
  
    var inputRef = React.useRef(null);
  
    var _React$useState = React.useState(null),
        displayNode = _React$useState[0],
        setDisplayNode = _React$useState[1];
  
    var _React$useRef = React.useRef(openProp != null),
        isOpenControlled = _React$useRef.current;
  
    var _React$useState2 = React.useState(),
        menuMinWidthState = _React$useState2[0],
        setMenuMinWidthState = _React$useState2[1];
  
    var _React$useState3 = React.useState(false),
        openState = _React$useState3[0],
        setOpenState = _React$useState3[1];
  
    var handleRef = useForkRef(ref, inputRefProp);
    React.useImperativeHandle(handleRef, function () {
      return {
        focus: function focus() {
          displayNode.focus();
        },
        node: inputRef.current,
        value: value
      };
    }, [displayNode, value]);
    React.useEffect(function () {
      if (autoFocus && displayNode) {
        displayNode.focus();
      }
    }, [autoFocus, displayNode]);
  
    var update = function update(open, event) {
      if (open) {
        if (onOpen) {
          onOpen(event);
        }
      } else {
        displayNode.focus();
  
        if (onClose) {
          onClose(event);
        }
      }
  
      if (!isOpenControlled) {
        setMenuMinWidthState(autoWidth ? null : displayNode.clientWidth);
        setOpenState(open);
      }
    };
  
    var handleClick = function handleClick(event) {
      update(true, event);
    };
  
    var handleClose = function handleClose(event) {
      update(false, event);
    };
  
    var handleItemClick = function handleItemClick(child) {
      return function (event) {
        if (!multiple) {
          update(false, event);
        }
  
        if (onChange) {
          var newValue;
  
          if (multiple) {
            newValue = Array.isArray(value) ? _toConsumableArray(value) : [];
            var itemIndex = value.indexOf(child.props.value);
  
            if (itemIndex === -1) {
              newValue.push(child.props.value);
            } else {
              newValue.splice(itemIndex, 1);
            }
          } else {
            newValue = child.props.value;
          }
  
          event.persist();
          event.target = {
            value: newValue,
            name: name
          };
          onChange(event, child);
        }
      };
    };
  
    var handleKeyDown = function handleKeyDown(event) {
      if (!readOnly) {
        var validKeys = [' ', 'ArrowUp', 'ArrowDown', // The native select doesn't respond to enter on MacOS, but it's recommended by
        // https://www.w3.org/TR/wai-aria-practices/examples/listbox/listbox-collapsible.html
        'Enter'];
  
        if (validKeys.indexOf(event.key) !== -1) {
          event.preventDefault();
          update(true, event);
        }
      }
    };
  
    var open = displayNode !== null && (isOpenControlled ? openProp : openState);
  
    var handleBlur = function handleBlur(event) {
      // if open event.stopImmediatePropagation
      if (!open && onBlur) {
        event.persist();
        event.target = {
          value: value,
          name: name
        };
        onBlur(event);
      }
    };
  
    delete other['aria-invalid'];
    var display;
    var displaySingle;
    var displayMultiple = [];
    var computeDisplay = false;
    var foundMatch = false; // No need to display any value if the field is empty.
  
    if (isFilled(props) || displayEmpty) {
      if (renderValue) {
        display = renderValue(value);
      } else {
        computeDisplay = true;
      }
    }
  
    var items = React.Children.map(children, function (child) {
      if (!React.isValidElement(child)) {
        return null;
      }
  
      if (process.env.NODE_ENV !== 'production') {
        if (child.type === React.Fragment) {
          console.error(["Material-UI: the Select component doesn't accept a Fragment as a child.", 'Consider providing an array instead.'].join('\n'));
        }
      }
  
      var selected;
  
      if (multiple) {
        if (!Array.isArray(value)) {
          throw new Error('Material-UI: the `value` prop must be an array ' + 'when using the `Select` component with `multiple`.');
        }
  
        selected = value.some(function (v) {
          return areEqualValues(v, child.props.value);
        });
  
        if (selected && computeDisplay) {
          displayMultiple.push(child.props.children);
        }
      } else {
        selected = areEqualValues(value, child.props.value);
  
        if (selected && computeDisplay) {
          displaySingle = child.props.children;
        }
      }
  
      if (selected) {
        foundMatch = true;
      }
  
      return React.cloneElement(child, {
        'aria-selected': selected ? 'true' : undefined,
        onClick: handleItemClick(child),
        role: 'option',
        selected: selected,
        value: undefined,
        // The value is most likely not a valid HTML attribute.
        'data-value': child.props.value // Instead, we provide it as a data attribute.
  
      });
    });
  
    if (process.env.NODE_ENV !== 'production') {
      // eslint-disable-next-line react-hooks/rules-of-hooks
      React.useEffect(function () {
        if (!foundMatch && !multiple && value !== '') {
          var values = React.Children.toArray(children).map(function (child) {
            return child.props.value;
          });
          console.warn(["Material-UI: you have provided an out-of-range value `".concat(value, "` for the select ").concat(name ? "(name=\"".concat(name, "\") ") : '', "component."), "Consider providing a value that matches one of the available options or ''.", "The available values are ".concat(values.join(', ') || '""', ".")].join('\n'));
        }
      }, [foundMatch, children, multiple, name, value]);
    }
  
    if (computeDisplay) {
      display = multiple ? displayMultiple.join(', ') : displaySingle;
    } // Avoid performing a layout computation in the render method.
  
  
    var menuMinWidth = menuMinWidthState;
  
    if (!autoWidth && isOpenControlled && displayNode) {
      menuMinWidth = displayNode.clientWidth;
    }
  
    var tabIndex;
  
    if (typeof tabIndexProp !== 'undefined') {
      tabIndex = tabIndexProp;
    } else {
      tabIndex = disabled ? null : 0;
    }
  
    return React.createElement(React.Fragment, null, React.createElement("div", _extends({
      className: clsx(classes.root, // TODO v5: merge root and select
      classes.select, classes.selectMenu, classes[variant], className, disabled && classes.disabled),
      ref: setDisplayNode,
      tabIndex: tabIndex,
      role: "button",
      "aria-expanded": open ? 'true' : undefined,
      "aria-haspopup": "listbox",
      "aria-owns": open ? "menu-".concat(name || '') : undefined,
      onKeyDown: handleKeyDown,
      onClick: disabled || readOnly ? null : handleClick,
      onBlur: handleBlur,
      onFocus: onFocus // The id can help with end-to-end testing automation.
      ,
      id: name ? "select-".concat(name) : undefined
    }, SelectDisplayProps), isEmpty(display) ? // eslint-disable-next-line react/no-danger
    React.createElement("span", {
      dangerouslySetInnerHTML: {
        __html: '&#8203;'
      }
    }) : display), React.createElement("input", _extends({
      value: Array.isArray(value) ? value.join(',') : value,
      name: name,
      ref: inputRef,
      type: "hidden",
      autoFocus: autoFocus
    }, other)), React.createElement(IconComponent, {
      className: clsx(classes.icon, classes["icon".concat(capitalize(variant))])
    }), React.createElement(Menu, _extends({
      id: "menu-".concat(name || ''),
      anchorEl: displayNode,
      open: open,
      onClose: handleClose
    }, MenuProps, {
      MenuListProps: _extends({
        role: 'listbox',
        disableListWrap: true
      }, MenuProps.MenuListProps),
      PaperProps: _extends({}, MenuProps.PaperProps, {
        style: _extends({
          minWidth: menuMinWidth
        }, MenuProps.PaperProps != null ? MenuProps.PaperProps.style : null)
      })
    }), items));
  });
  process.env.NODE_ENV !== "production" ? SelectInput.propTypes = {
    /**
     * @ignore
     */
    autoFocus: PropTypes.bool,
  
    /**
     * If true, the width of the popover will automatically be set according to the items inside the
     * menu, otherwise it will be at least the width of the select input.
     */
    autoWidth: PropTypes.bool,
  
    /**
     * The option elements to populate the select with.
     * Can be some `<MenuItem>` elements.
     */
    children: PropTypes.node,
  
    /**
     * Override or extend the styles applied to the component.
     * See [CSS API](#css) below for more details.
     */
    classes: PropTypes.object.isRequired,
  
    /**
     * The CSS class name of the select element.
     */
    className: PropTypes.string,
  
    /**
     * If `true`, the select will be disabled.
     */
    disabled: PropTypes.bool,
  
    /**
     * If `true`, the selected item is displayed even if its value is empty.
     */
    displayEmpty: PropTypes.bool,
  
    /**
     * The icon that displays the arrow.
     */
    IconComponent: PropTypes.elementType.isRequired,
  
    /**
     * Imperative handle implementing `{ value: T, node: HTMLElement, focus(): void }`
     * Equivalent to `ref`
     */
    inputRef: refType,
  
    /**
     * Props applied to the [`Menu`](/api/menu/) element.
     */
    MenuProps: PropTypes.object,
  
    /**
     * If true, `value` must be an array and the menu will support multiple selections.
     */
    multiple: PropTypes.bool,
  
    /**
     * Name attribute of the `select` or hidden `input` element.
     */
    name: PropTypes.string,
  
    /**
     * @ignore
     */
    onBlur: PropTypes.func,
  
    /**
     * Callback function fired when a menu item is selected.
     *
     * @param {object} event The event source of the callback.
     * You can pull out the new value by accessing `event.target.value` (any).
     * @param {object} [child] The react element that was selected.
     */
    onChange: PropTypes.func,
  
    /**
     * Callback fired when the component requests to be closed.
     * Use in controlled mode (see open).
     *
     * @param {object} event The event source of the callback.
     */
    onClose: PropTypes.func,
  
    /**
     * @ignore
     */
    onFocus: PropTypes.func,
  
    /**
     * Callback fired when the component requests to be opened.
     * Use in controlled mode (see open).
     *
     * @param {object} event The event source of the callback.
     */
    onOpen: PropTypes.func,
  
    /**
     * Control `select` open state.
     */
    open: PropTypes.bool,
  
    /**
     * @ignore
     */
    readOnly: PropTypes.bool,
  
    /**
     * Render the selected value.
     *
     * @param {*} value The `value` provided to the component.
     * @returns {ReactElement}
     */
    renderValue: PropTypes.func,
  
    /**
     * @ignore
     */
    required: PropTypes.bool,
  
    /**
     * Props applied to the clickable div element.
     */
    SelectDisplayProps: PropTypes.object,
  
    /**
     * @ignore
     */
    tabIndex: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
  
    /**
     * @ignore
     */
    type: PropTypes.any,
  
    /**
     * The input value.
     */
    value: PropTypes.any.isRequired,
  
    /**
     * The variant to use.
     */
    variant: PropTypes.oneOf(['standard', 'outlined', 'filled'])
  } : void 0;
  export default SelectInput;