var _excluded = ["value"],
  _excluded2 = ["block", "children", "className", "clearRef", "disabled", "error", "focus", "label", "loading", "onClear", "open", "placeholder", "qa", "tabIndex", "isMenuOpened"],
  _excluded3 = ["onSearch", "header", "footer", "emptyMessage", "optionsRef", "children", "i18nScope", "onSelect", "menuRef"],
  _excluded4 = ["afterHide", "afterShow", "beforeHide", "beforeShow", "block", "children", "className", "container", "disabled", "emptyMessage", "error", "header", "footer", "hideDelay", "i18nScope", "label", "loading", "onClear", "onKeyDown", "onScrollBottom", "onSearch", "onSelect", "optionsRef", "placeholder", "placement", "restoreFocusOnHide", "showDelay", "tabIndex", "qa"];
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
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(r) { if (Array.isArray(r)) return r; }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
import { Clear } from '@procore/core-icons/dist';
import classNames from 'classnames';
import { isNil } from 'ramda';
import React from 'react';
import { Box } from '../Box';
import { MenuImperative, useMenuImperativeControlNavigation } from '../MenuImperative/MenuImperative';
import { Notation } from '../Notation';
import { OverlayTrigger, useOverlayTriggerContext } from '../OverlayTrigger/OverlayTrigger';
import { Spinner } from '../Spinner';
import { Tooltip } from '../Tooltip';
import { useI18nContext } from '../_hooks/I18n';
import { OverflowObserver } from '../_hooks/OverflowObserver';
import { addSubcomponents } from '../_utils/addSubcomponents';
import { StyledSelectArrow, StyledSelectArrowContainer, StyledSelectButton, StyledSelectButtonLabel, StyledSelectClearIcon, StyledSelectMenu, StyledSelectSpinner } from './Select.styles';
function noop() {}
function isFunction(obj) {
  return typeof obj === 'function';
}
function isOpen(_ref) {
  var _ref$open = _ref.open,
    open = _ref$open === void 0 ? false : _ref$open;
  return open;
}
export var OptGroup = /*#__PURE__*/React.forwardRef(function (props, ref) {
  return /*#__PURE__*/React.createElement(MenuImperative.Group, _extends({}, props, {
    clickable: false,
    ref: ref
  }));
});
export var Option = /*#__PURE__*/React.forwardRef(function (_ref2, ref) {
  var value = _ref2.value,
    props = _objectWithoutProperties(_ref2, _excluded);
  return /*#__PURE__*/React.createElement(MenuImperative.Item, _extends({
    ref: ref
  }, props, {
    item: value
  }));
});
export var Button = /*#__PURE__*/React.forwardRef(function Button(_ref3, ref) {
  var _ref3$block = _ref3.block,
    block = _ref3$block === void 0 ? false : _ref3$block,
    children = _ref3.children,
    className = _ref3.className,
    clearRef = _ref3.clearRef,
    _ref3$disabled = _ref3.disabled,
    disabled = _ref3$disabled === void 0 ? false : _ref3$disabled,
    _ref3$error = _ref3.error,
    error = _ref3$error === void 0 ? false : _ref3$error,
    _ref3$focus = _ref3.focus,
    focus = _ref3$focus === void 0 ? false : _ref3$focus,
    label = _ref3.label,
    _ref3$loading = _ref3.loading,
    loading = _ref3$loading === void 0 ? false : _ref3$loading,
    onClear = _ref3.onClear,
    _ref3$open = _ref3.open,
    open = _ref3$open === void 0 ? false : _ref3$open,
    placeholder = _ref3.placeholder,
    qa = _ref3.qa,
    _ref3$tabIndex = _ref3.tabIndex,
    tabIndex = _ref3$tabIndex === void 0 ? 0 : _ref3$tabIndex,
    _ref3$isMenuOpened = _ref3.isMenuOpened,
    isMenuOpened = _ref3$isMenuOpened === void 0 ? false : _ref3$isMenuOpened,
    props = _objectWithoutProperties(_ref3, _excluded2);
  var i18n = useI18nContext();
  var hasClearIcon = !disabled && !loading && Boolean(label);
  var content = children || label || placeholder;
  return /*#__PURE__*/React.createElement(StyledSelectButton, _extends({
    className: classNames(className, {
      focus: isFunction(focus) ? focus({
        open: open
      }) : focus
    }),
    ref: ref,
    role: "button",
    tabIndex: disabled ? -1 : tabIndex,
    $block: block,
    $error: error,
    $disabled: disabled,
    $hasClearIcon: hasClearIcon,
    $loading: loading,
    $open: open,
    $placeholder: !label
  }, props), /*#__PURE__*/React.createElement(OverflowObserver, null, function (_ref4) {
    var isOverflowingX = _ref4.isOverflowingX,
      overflowRef = _ref4.ref;
    var showTooltip = !disabled && isOverflowingX && !isMenuOpened;
    var trigger = /*#__PURE__*/React.createElement(StyledSelectButtonLabel, {
      "data-qa": qa === null || qa === void 0 ? void 0 : qa.label,
      $hoverable: showTooltip,
      ref: overflowRef
    }, content);
    return showTooltip ? /*#__PURE__*/React.createElement(Tooltip, {
      trigger: "hover",
      overlay: content
    }, trigger) : trigger;
  }), onClear && /*#__PURE__*/React.createElement(StyledSelectClearIcon, {
    "aria-hidden": true // prevents reading "delete field" button when select in focus
    ,
    "aria-label": i18n.t('core.select.clear'),
    ref: clearRef,
    "data-qa": (qa === null || qa === void 0 ? void 0 : qa.clear) || 'core-select-clear',
    size: "sm",
    variant: "tertiary",
    icon: /*#__PURE__*/React.createElement(Clear, null),
    onClick: onClear,
    onMouseDown: function onMouseDown(e) {
      return e.preventDefault();
    } // prevents an element from getting the focus
    ,
    tabIndex: -1 // TODO revisit this accessibility
  }), loading ? /*#__PURE__*/React.createElement(StyledSelectSpinner, null, /*#__PURE__*/React.createElement(Spinner, {
    color: "blue50",
    size: "xs"
  })) : /*#__PURE__*/React.createElement(StyledSelectArrowContainer, null, /*#__PURE__*/React.createElement(StyledSelectArrow, null)));
});
var SelectMenu = /*#__PURE__*/React.forwardRef(function SelectMenu(_ref5, ref) {
  var onSearch_ = _ref5.onSearch,
    header = _ref5.header,
    footer = _ref5.footer,
    emptyMessage = _ref5.emptyMessage,
    optionsRef = _ref5.optionsRef,
    children = _ref5.children,
    i18nScope = _ref5.i18nScope,
    _ref5$onSelect = _ref5.onSelect,
    onSelect_ = _ref5$onSelect === void 0 ? noop : _ref5$onSelect,
    menuRef = _ref5.menuRef,
    props = _objectWithoutProperties(_ref5, _excluded3);
  var ctx = useOverlayTriggerContext();
  var _React$useState = React.useState(''),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    searchValue = _React$useState2[0],
    setSearchValue = _React$useState2[1];
  var _useMenuImperativeCon = useMenuImperativeControlNavigation(menuRef, Boolean(onSearch_)),
    menuProps = _useMenuImperativeCon.menuProps,
    menuNavigationTriggerProps = _useMenuImperativeCon.menuNavigationTriggerProps;
  React.useEffect(function () {
    var _menuRef$current;
    (_menuRef$current = menuRef.current) === null || _menuRef$current === void 0 ? void 0 : _menuRef$current.highlightFirst();
  }, [menuRef, searchValue]);
  React.useEffect(function () {
    var _menuRef$current2, _menuRef$current3;
    (_menuRef$current2 = menuRef.current) === null || _menuRef$current2 === void 0 ? void 0 : _menuRef$current2.highlightSuggested();
    (_menuRef$current3 = menuRef.current) === null || _menuRef$current3 === void 0 ? void 0 : _menuRef$current3.highlightSelected();
  }, [menuRef]);
  function onKeyDown(e) {
    var _props$onKeyDown;
    (_props$onKeyDown = props.onKeyDown) === null || _props$onKeyDown === void 0 ? void 0 : _props$onKeyDown.call(props, e);
    // This is disconnected from the OverlayTrigger closing on Escape.
    // Closing a dropdown with Escape will not close a Modal, because stopP.
    // It is called only once because the menu component is unmounted.
    // The next Escape will propagate.
    if (e.key === 'Escape' || e.key === 'Esc') {
      e.stopPropagation();
    }
  }
  function onSelect(selection) {
    onSelect_(selection);
    ctx.hide(selection.event);
  }
  function onSearch(event) {
    setSearchValue(event.target.value);
    onSearch_ === null || onSearch_ === void 0 ? void 0 : onSearch_(event);
  }
  return /*#__PURE__*/React.createElement(StyledSelectMenu, {
    ref: ref,
    shadowStrength: 2
  }, /*#__PURE__*/React.createElement(MenuImperative, _extends({}, props, menuProps, {
    ref: menuRef,
    role: "listbox",
    onKeyDown: onKeyDown,
    onSelect: onSelect,
    circular: true
  }), onSearch_ && /*#__PURE__*/React.createElement(MenuImperative.Search, _extends({
    autoComplete: "false",
    i18nScope: i18nScope,
    onChange: onSearch
  }, menuNavigationTriggerProps)), header && /*#__PURE__*/React.createElement(MenuImperative.Header, null, header), React.Children.count(children) ? /*#__PURE__*/React.createElement(MenuImperative.Options, {
    ref: optionsRef
  }, children) : /*#__PURE__*/React.createElement(Box, {
    padding: "md lg"
  }, /*#__PURE__*/React.createElement(Notation, {
    variant: "pagination"
  }, emptyMessage)), footer && /*#__PURE__*/React.createElement(MenuImperative.Footer, null, footer)));
});
var Select_ = /*#__PURE__*/React.forwardRef(function Select(_ref6, forwardRef) {
  var _ref6$afterHide = _ref6.afterHide,
    afterHide_ = _ref6$afterHide === void 0 ? noop : _ref6$afterHide,
    _ref6$afterShow = _ref6.afterShow,
    afterShow_ = _ref6$afterShow === void 0 ? noop : _ref6$afterShow,
    _ref6$beforeHide = _ref6.beforeHide,
    _beforeHide = _ref6$beforeHide === void 0 ? function () {
      return true;
    } : _ref6$beforeHide,
    _ref6$beforeShow = _ref6.beforeShow,
    beforeShow = _ref6$beforeShow === void 0 ? function () {
      return true;
    } : _ref6$beforeShow,
    _ref6$block = _ref6.block,
    block = _ref6$block === void 0 ? false : _ref6$block,
    children = _ref6.children,
    className = _ref6.className,
    container = _ref6.container,
    _ref6$disabled = _ref6.disabled,
    disabled = _ref6$disabled === void 0 ? false : _ref6$disabled,
    emptyMessage = _ref6.emptyMessage,
    _ref6$error = _ref6.error,
    error = _ref6$error === void 0 ? false : _ref6$error,
    header = _ref6.header,
    footer = _ref6.footer,
    _ref6$hideDelay = _ref6.hideDelay,
    hideDelay = _ref6$hideDelay === void 0 ? 100 : _ref6$hideDelay,
    _ref6$i18nScope = _ref6.i18nScope,
    i18nScope = _ref6$i18nScope === void 0 ? 'core.select' : _ref6$i18nScope,
    _ref6$label = _ref6.label,
    label = _ref6$label === void 0 ? '' : _ref6$label,
    _ref6$loading = _ref6.loading,
    loading = _ref6$loading === void 0 ? false : _ref6$loading,
    onClear_ = _ref6.onClear,
    _ref6$onKeyDown = _ref6.onKeyDown,
    onKeyDown = _ref6$onKeyDown === void 0 ? noop : _ref6$onKeyDown,
    onScrollBottom = _ref6.onScrollBottom,
    onSearch = _ref6.onSearch,
    _ref6$onSelect = _ref6.onSelect,
    onSelect = _ref6$onSelect === void 0 ? noop : _ref6$onSelect,
    optionsRef = _ref6.optionsRef,
    placeholder = _ref6.placeholder,
    _ref6$placement = _ref6.placement,
    placement = _ref6$placement === void 0 ? 'bottom-left' : _ref6$placement,
    _ref6$restoreFocusOnH = _ref6.restoreFocusOnHide,
    restoreFocusOnHide = _ref6$restoreFocusOnH === void 0 ? 'core-react' : _ref6$restoreFocusOnH,
    _ref6$showDelay = _ref6.showDelay,
    showDelay = _ref6$showDelay === void 0 ? 0 : _ref6$showDelay,
    _ref6$tabIndex = _ref6.tabIndex,
    tabIndex = _ref6$tabIndex === void 0 ? 0 : _ref6$tabIndex,
    qa = _ref6.qa,
    props = _objectWithoutProperties(_ref6, _excluded4);
  var i18n = useI18nContext();
  var menuRef = React.useRef(null);
  var overlayTriggerRef = React.useRef(null);
  var ref = forwardRef || overlayTriggerRef;
  var _React$useState3 = React.useState(false),
    _React$useState4 = _slicedToArray(_React$useState3, 2),
    isMenuOpened = _React$useState4[0],
    setIsMenuOpened = _React$useState4[1];
  var clearRef = React.useRef(null);
  var emptyMessageText = emptyMessage !== null && emptyMessage !== void 0 ? emptyMessage : i18n.t('noResult', {
    scope: i18nScope
  });
  function afterHide() {
    afterHide_();
    setIsMenuOpened(false);
    onSearch === null || onSearch === void 0 ? void 0 : onSearch({
      target: {
        value: ''
      }
    });
  }
  var onClear = React.useCallback(function (e) {
    var _overlayTriggerRef$cu;
    onClear_ === null || onClear_ === void 0 ? void 0 : onClear_(e);
    // IE 11 fix
    // Return focus back to trigger instead of the clear button
    overlayTriggerRef === null || overlayTriggerRef === void 0 ? void 0 : (_overlayTriggerRef$cu = overlayTriggerRef.current) === null || _overlayTriggerRef$cu === void 0 ? void 0 : _overlayTriggerRef$cu.focus();
  }, [onClear_]);
  function afterShow() {
    afterShow_();
    setIsMenuOpened(true);
  }
  return /*#__PURE__*/React.createElement(OverlayTrigger, {
    autoFocus: true,
    afterHide: afterHide,
    afterShow: afterShow,
    beforeHide: function beforeHide(e) {
      if (e.target === clearRef.current) {
        return false;
      }
      return _beforeHide(e);
    },
    beforeShow: beforeShow,
    canFlip: true,
    container: container,
    hideDelay: hideDelay,
    restoreFocusOnHide: restoreFocusOnHide,
    overlay: /*#__PURE__*/React.createElement(SelectMenu, {
      onSearch: onSearch,
      header: header,
      footer: footer,
      emptyMessage: emptyMessageText,
      optionsRef: optionsRef,
      i18nScope: i18nScope,
      onSelect: onSelect,
      onScrollBottom: onScrollBottom,
      menuRef: menuRef
    }, children),
    placement: placement,
    ref: ref,
    showDelay: showDelay,
    trigger: disabled ? 'none' : 'click'
  }, /*#__PURE__*/React.createElement(Button, _extends({}, props, {
    block: block,
    className: className,
    clearRef: clearRef,
    disabled: disabled,
    error: error,
    focus: onSearch ? false : isOpen,
    label: label,
    loading: loading,
    placeholder: placeholder,
    onKeyDown: onKeyDown,
    onClear: isNil(onClear_) ? onClear_ : onClear,
    isMenuOpened: isMenuOpened,
    tabIndex: tabIndex,
    qa: qa
  })));
});
Select_.displayName = 'Select';
Button.displayName = 'Select.Button';
Option.displayName = 'Select.Option';
OptGroup.displayName = 'Select.OptGroup';

/**

 We use single selects to allow our users to choose a single option from a list,
 presented in a dropdown. We typically see these selects on forms.

 If you want users to select multiple options, use a multi select, group select,
 and tiered select if you want users to select from a tiered set of options,
 use a tiered select.

 @since 10.19.0

 @see [Storybook](https://stories.core.procore.com/?path=/story/core-react_demos-select--demo)

 @see [Design Guidelines](https://design.procore.com/select)

 */
export var Select = addSubcomponents({
  Button: Button,
  Option: Option,
  OptGroup: OptGroup
}, Select_);
//# sourceMappingURL=Select.js.map