"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _drop = _interopRequireDefault(require("../drop"));
var _useClonedChildren = _interopRequireDefault(require("../mixins/useClonedChildren"));
var _getAncestors = _interopRequireDefault(require("../mixins/getAncestors"));
var _useForwardRef2 = _interopRequireDefault(require("../../../hooks/useForwardRef"));
var _useToggle2 = _interopRequireDefault(require("../../../hooks/useToggle"));
var _menuButton = _interopRequireDefault(require("./menuButton"));
var _dropdown = _interopRequireDefault(require("./dropdown"));
var _dropdownItem = _interopRequireDefault(require("./dropdownItem"));
var _excluded = ["value", "onChange", "onOpen", "onClose", "closeOnClick", "open", "icon", "label", "caret", "children", "dropProps", "dropdownProps", "itemProps", "items", "Item", "Footer", "Dropdown", "animation", "dropTitle", "dropTitlePadding", "hasSearch", "ref"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
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); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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; }
var Menu = function Menu(_ref) {
  var value = _ref.value,
    onChange = _ref.onChange,
    onOpen = _ref.onOpen,
    onClose = _ref.onClose,
    _ref$closeOnClick = _ref.closeOnClick,
    closeOnClick = _ref$closeOnClick === void 0 ? true : _ref$closeOnClick,
    _ref$open = _ref.open,
    initialOpen = _ref$open === void 0 ? false : _ref$open,
    _ref$icon = _ref.icon,
    icon = _ref$icon === void 0 ? null : _ref$icon,
    label = _ref.label,
    _ref$caret = _ref.caret,
    caret = _ref$caret === void 0 ? true : _ref$caret,
    children = _ref.children,
    dropProps = _ref.dropProps,
    dropdownProps = _ref.dropdownProps,
    itemProps = _ref.itemProps,
    items = _ref.items,
    _ref$Item = _ref.Item,
    Item = _ref$Item === void 0 ? _dropdownItem["default"] : _ref$Item,
    Footer = _ref.Footer,
    _ref$Dropdown = _ref.Dropdown,
    Dropdown = _ref$Dropdown === void 0 ? _dropdown["default"] : _ref$Dropdown,
    animation = _ref.animation,
    dropTitle = _ref.dropTitle,
    dropTitlePadding = _ref.dropTitlePadding,
    _ref$hasSearch = _ref.hasSearch,
    hasSearch = _ref$hasSearch === void 0 ? false : _ref$hasSearch,
    parentRef = _ref.ref,
    rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  var _useToggle = (0, _useToggle2["default"])(initialOpen, {
      on: onOpen,
      off: onClose
    }),
    isOpen = _useToggle[0],
    toggleIsOpen = _useToggle[1],
    close = _useToggle[3];
  var _useForwardRef = (0, _useForwardRef2["default"])(parentRef),
    ref = _useForwardRef[0],
    setRef = _useForwardRef[1];
  var onItemClick = (0, _react.useCallback)(function (nextValue) {
    if (onChange) onChange(nextValue);
    if (closeOnClick) close();
  }, [onChange]);
  var onClickOutside = (0, _react.useCallback)(function (event) {
    if (ref.current !== event.target && !(0, _getAncestors["default"])(event.target).some(function (node) {
      return node === ref.current;
    })) {
      close();
    }
  }, [close]);
  var clonedChildren = (0, _useClonedChildren["default"])(children, setRef, _objectSpread({
    onClick: toggleIsOpen,
    "aria-haspopup": "listbox",
    "aria-expanded": isOpen,
    open: isOpen
  }, rest));
  var menuLabel = (0, _react.useMemo)(function () {
    if (label) return label;
    if (clonedChildren) return clonedChildren;
    var item = items.find(function (i) {
      return i.value === value;
    });
    return item == null ? void 0 : item.label;
  }, [label, clonedChildren, items, value]);
  return /*#__PURE__*/_react["default"].createElement(_react.Fragment, null, clonedChildren || /*#__PURE__*/_react["default"].createElement(_menuButton["default"], _extends({
    ref: setRef,
    icon: icon,
    label: menuLabel,
    caret: caret,
    onClick: toggleIsOpen,
    open: isOpen
  }, rest)), isOpen && ref.current && /*#__PURE__*/_react["default"].createElement(_drop["default"], _extends({
    animation: animation,
    onEsc: close,
    onClickOutside: onClickOutside,
    hideShadow: true,
    target: ref.current
  }, dropProps), /*#__PURE__*/_react["default"].createElement(Dropdown, _extends({
    dropTitle: dropTitle,
    dropTitlePadding: dropTitlePadding,
    value: value,
    onItemClick: onItemClick,
    items: items,
    itemProps: itemProps,
    Item: Item,
    Footer: Footer,
    hasSearch: hasSearch,
    close: close
  }, dropdownProps))));
};
var _default = exports["default"] = Menu;