var _excluded = ["restoreFocus"],
  _excluded2 = ["children", "className", "arrow", "loading", "open", "focus", "variant"],
  _excluded3 = ["afterHide", "afterShow", "beforeHide", "beforeShow", "children", "error", "footer", "i18nScope", "icon", "label", "onKeyDown", "onSearch", "onSelect", "placement", "restoreFocusOnHide", "variant", "hasTabbableElements"];
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(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
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 _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 { CaretDown, CaretUp, EllipsisVertical } from '@procore/core-icons/dist';
import classNames from 'classnames';
import React from 'react';
import { MenuImperative, useMenuImperativeControlNavigation } from '../MenuImperative/MenuImperative';
import { OverlayTrigger, useOverlayTriggerContext } from '../OverlayTrigger/OverlayTrigger';
import { useI18nContext } from '../_hooks/I18n';
import { addSubcomponents } from '../_utils/addSubcomponents';
import { mergeRefs } from '../_utils/mergeRefs';
import { StyledDropdownButton, StyledDropdownMenu, StyledDropdownSpinner } from './Dropdown.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;
}
var Item = /*#__PURE__*/React.forwardRef(function Item(_ref2, ref) {
  var _ref2$restoreFocus = _ref2.restoreFocus,
    restoreFocus = _ref2$restoreFocus === void 0 ? false : _ref2$restoreFocus,
    props = _objectWithoutProperties(_ref2, _excluded);
  return /*#__PURE__*/React.createElement(MenuImperative.Item, _extends({
    ref: ref,
    restoreFocus: restoreFocus
  }, props));
});
export var DropdownButton = /*#__PURE__*/React.forwardRef(function DropdownButton(_ref3, ref) {
  var children = _ref3.children,
    className = _ref3.className,
    _ref3$arrow = _ref3.arrow,
    arrow = _ref3$arrow === void 0 ? false : _ref3$arrow,
    _ref3$loading = _ref3.loading,
    loading = _ref3$loading === void 0 ? false : _ref3$loading,
    _ref3$open = _ref3.open,
    open = _ref3$open === void 0 ? false : _ref3$open,
    _ref3$focus = _ref3.focus,
    focus = _ref3$focus === void 0 ? false : _ref3$focus,
    _ref3$variant = _ref3.variant,
    variant = _ref3$variant === void 0 ? 'secondary' : _ref3$variant,
    props = _objectWithoutProperties(_ref3, _excluded2);
  var caret = arrow && (open ? /*#__PURE__*/React.createElement(CaretUp, {
    size: "sm"
  }) : /*#__PURE__*/React.createElement(CaretDown, {
    size: "sm"
  }));
  var icon = loading ? /*#__PURE__*/React.createElement(StyledDropdownSpinner, {
    size: "xs",
    variant: variant === 'primary' ? 'light' : ''
  }) : caret;
  return /*#__PURE__*/React.createElement(StyledDropdownButton, _extends({}, props, {
    className: classNames(className, {
      focus: isFunction(focus) ? focus({
        open: open
      }) : focus
    }),
    "aria-busy": loading || undefined,
    iconRight: icon,
    $loading: loading,
    variant: variant,
    ref: ref
  }), children);
});
var DropdownMenu = /*#__PURE__*/React.forwardRef(function DropdownMenu(_ref4, ref) {
  var children = _ref4.children,
    footer = _ref4.footer,
    i18nScope = _ref4.i18nScope,
    menuRef = _ref4.menuRef,
    onSearch_ = _ref4.onSearch,
    _ref4$onSelect = _ref4.onSelect,
    onSelect_ = _ref4$onSelect === void 0 ? noop : _ref4$onSelect,
    id = _ref4.id;
  var overlayTriggerContext = useOverlayTriggerContext();
  var _React$useState = React.useState(''),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    search = _React$useState2[0],
    setSearch = _React$useState2[1];
  var _useMenuImperativeCon = useMenuImperativeControlNavigation(menuRef, Boolean(onSearch_), {
      menuId: id
    }),
    menuProps = _useMenuImperativeCon.menuProps,
    menuNavigationTriggerProps = _useMenuImperativeCon.menuNavigationTriggerProps;
  function onSelect(selection) {
    overlayTriggerContext.hide({
      restoreFocus: selection.restoreFocus
    });
    onSelect_(selection);
  }
  function onSearch(e) {
    setSearch(e.target.value);
    onSearch_ === null || onSearch_ === void 0 ? void 0 : onSearch_(e);
  }
  React.useEffect(function () {
    var _menuRef$current;
    (_menuRef$current = menuRef.current) === null || _menuRef$current === void 0 ? void 0 : _menuRef$current.highlightFirst();
  }, [menuRef, search]);
  function onKeyDown(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') {
      // only let parent listeners (modal, overlay) know when empty
      e.stopPropagation();
    }
  }
  return /*#__PURE__*/React.createElement(StyledDropdownMenu, {
    ref: ref,
    shadowStrength: 2
  }, /*#__PURE__*/React.createElement(MenuImperative, _extends({}, menuProps, {
    ref: menuRef,
    onKeyDown: onKeyDown,
    onSelect: onSelect,
    role: "menu"
  }), onSearch_ && /*#__PURE__*/React.createElement(MenuImperative.Search, _extends({}, menuNavigationTriggerProps, {
    onChange: onSearch,
    i18nScope: i18nScope
  })), /*#__PURE__*/React.createElement(MenuImperative.Options, null, children), footer && /*#__PURE__*/React.createElement(MenuImperative.Footer, null, footer)));
});

/**

 Dropdowns display a list of actions in a menu that opens and closes. We use
 dropdowns most commonly to trigger an action or to redirect the user to
 a new page or modal.

 @since 10.19.0

 @see [Storybook](https://procore.github.io/core/core-react/latest/?path=/story/demos-dropdown--demo)

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

 */
var Dropdown_ = /*#__PURE__*/React.forwardRef(function Dropdown(_ref5, ref) {
  var afterHide_ = _ref5.afterHide,
    afterShow = _ref5.afterShow,
    beforeHide = _ref5.beforeHide,
    beforeShow = _ref5.beforeShow,
    children = _ref5.children,
    error = _ref5.error,
    footer = _ref5.footer,
    _ref5$i18nScope = _ref5.i18nScope,
    i18nScope = _ref5$i18nScope === void 0 ? 'core.dropdown' : _ref5$i18nScope,
    icon = _ref5.icon,
    label = _ref5.label,
    _ref5$onKeyDown = _ref5.onKeyDown,
    onKeyDown = _ref5$onKeyDown === void 0 ? noop : _ref5$onKeyDown,
    onSearch = _ref5.onSearch,
    _ref5$onSelect = _ref5.onSelect,
    onSelect = _ref5$onSelect === void 0 ? noop : _ref5$onSelect,
    _ref5$placement = _ref5.placement,
    placement = _ref5$placement === void 0 ? 'bottom-left' : _ref5$placement,
    _ref5$restoreFocusOnH = _ref5.restoreFocusOnHide,
    restoreFocusOnHide = _ref5$restoreFocusOnH === void 0 ? false : _ref5$restoreFocusOnH,
    _ref5$variant = _ref5.variant,
    variant = _ref5$variant === void 0 ? 'secondary' : _ref5$variant,
    _ref5$hasTabbableElem = _ref5.hasTabbableElements,
    hasTabbableElements = _ref5$hasTabbableElem === void 0 ? false : _ref5$hasTabbableElem,
    props = _objectWithoutProperties(_ref5, _excluded3);
  var menuRef = React.useRef(null);
  var buttonRef = React.useRef(null);
  var isDialog = Boolean(onSearch) || hasTabbableElements;
  function afterHide(e) {
    // @ts-expect-error
    if ((e === null || e === void 0 ? void 0 : e.key) === 'Escape' || e !== null && e !== void 0 && e.restoreFocus) {
      var _buttonRef$current;
      (_buttonRef$current = buttonRef.current) === null || _buttonRef$current === void 0 ? void 0 : _buttonRef$current.focus();
    }
    afterHide_ === null || afterHide_ === void 0 ? void 0 : afterHide_(e);
    onSearch === null || onSearch === void 0 ? void 0 : onSearch({
      target: {
        value: ''
      }
    });
  }
  var I18n = useI18nContext();
  var ariaLabel = props['aria-label'] || label || I18n.t("core.dropdown.".concat(props.loading ? 'loading' : 'moreOptions'));
  return /*#__PURE__*/React.createElement(OverlayTrigger, {
    role: isDialog ? 'dialog' : 'menu',
    passA11yPropsToOverlay: !isDialog,
    autoFocus: true,
    afterHide: afterHide,
    afterShow: afterShow,
    beforeHide: beforeHide,
    beforeShow: beforeShow,
    overlay: /*#__PURE__*/React.createElement(DropdownMenu, {
      footer: footer,
      i18nScope: i18nScope,
      menuRef: menuRef,
      onSearch: onSearch,
      onSelect: onSelect
    }, children),
    placement: placement,
    ref: mergeRefs(ref, buttonRef),
    restoreFocusOnHide: restoreFocusOnHide
  }, /*#__PURE__*/React.createElement(DropdownButton, _extends({}, props, {
    "aria-label": ariaLabel,
    arrow: label !== undefined,
    focus: onSearch ? false : isOpen,
    icon: props.loading && !label ? undefined : !icon && !label ? /*#__PURE__*/React.createElement(EllipsisVertical, null) : icon,
    onKeyDown: onKeyDown,
    variant: variant
  }), label));
});
export var useDropdownContext = useOverlayTriggerContext;
export function DropdownConsumer(_ref6) {
  var children = _ref6.children;
  return children(useDropdownContext());
}
Dropdown_.displayName = 'Dropdown';
Item.displayName = 'Dropdown.Item';
export var Dropdown = addSubcomponents({
  Item: Item,
  Group: MenuImperative.Group
}, Dropdown_);
//# sourceMappingURL=Dropdown.js.map