"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Menu = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _context = require("./context");
var _Styles = require("./Styles");
var _useForkRef = _interopRequireDefault(require("../../utils/useForkRef"));
var _Overlay = require("../Overlay");
var _Popper = require("../Popper");
var _Window = require("../Window");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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 && Object.prototype.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 _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; }
// source: https://twitter.com/mattpocockuk/status/1671908303918473217/photo/1
// eslint-disable-next-line @typescript-eslint/ban-types

var Menu = exports.Menu = function Menu(_ref) {
  var children = _ref.children,
    _ref$disableFocusLock = _ref.disableFocusLock,
    disableFocusLock = _ref$disableFocusLock === void 0 ? false : _ref$disableFocusLock,
    onMenuClick = _ref.onMenuClick,
    _ref$handleScroll = _ref.handleScroll,
    handleScroll = _ref$handleScroll === void 0 ? true : _ref$handleScroll,
    _ref$mode = _ref.mode,
    mode = _ref$mode === void 0 ? "normal" : _ref$mode,
    _ref$open = _ref.open,
    defaultOpen = _ref$open === void 0 ? false : _ref$open,
    onOpen = _ref.onOpen,
    onClose = _ref.onClose,
    onBeforeClose = _ref.onBeforeClose,
    _ref$position = _ref.position,
    position = _ref$position === void 0 ? "bottom-start" : _ref$position,
    target = _ref.target,
    className = _ref.className,
    menuClassName = _ref.menuClassName,
    popperClassName = _ref.popperClassName,
    backgroundElementClass = _ref.backgroundElementClass,
    _ref$windowAbsolutely = _ref.windowAbsolutelyPositioned,
    windowAbsolutelyPositioned = _ref$windowAbsolutely === void 0 ? false : _ref$windowAbsolutely,
    onPopperOpen = _ref.onPopperOpen;
  var _useState = (0, _react.useState)(defaultOpen),
    _useState2 = _slicedToArray(_useState, 2),
    open = _useState2[0],
    setOpen = _useState2[1];
  var _useState3 = (0, _react.useState)(),
    _useState4 = _slicedToArray(_useState3, 2),
    childNode = _useState4[0],
    setChildNode = _useState4[1];
  var elementRef = (0, _react.useRef)(null);
  var isOpenAtLeastOnce = (0, _react.useRef)(false);
  (0, _react.useEffect)(function () {
    setOpen(defaultOpen);
  }, [defaultOpen]);
  var handleRefRef = (0, _useForkRef.default)(target ? target.ref : undefined, setChildNode);
  var handleRef = (0, _useForkRef.default)(handleRefRef, elementRef);
  var handleOpen = (0, _react.useCallback)(function (event) {
    var _target$props;
    event === null || event === void 0 || event.preventDefault();
    event === null || event === void 0 || event.stopPropagation();
    setOpen(true);
    typeof onOpen === "function" && onOpen();
    typeof (target === null || target === void 0 || (_target$props = target.props) === null || _target$props === void 0 ? void 0 : _target$props.onClick) === "function" && target.props.onClick(event);
  }, [onOpen, target === null || target === void 0 ? void 0 : target.props]);
  var handleClose = (0, _react.useCallback)(function () {
    if (typeof onBeforeClose === "function" && !onBeforeClose()) {
      return;
    }
    setOpen(false);
    typeof onClose === "function" && onClose();
  }, [onClose, onBeforeClose]);
  var childProps = {
    forceClose: handleClose
  };
  var targetProps = {
    open,
    ref: handleRef,
    onClick: handleOpen
  };
  (0, _react.useEffect)(function () {
    if (open) {
      isOpenAtLeastOnce.current = true;
    } else if (!open && isOpenAtLeastOnce.current) {
      var _elementRef$current;
      (_elementRef$current = elementRef.current) === null || _elementRef$current === void 0 || _elementRef$current.focus();
      isOpenAtLeastOnce.current = false;
    }
  }, [open]);
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, target && /*#__PURE__*/_react.default.cloneElement(target, targetProps), open ? /*#__PURE__*/_react.default.createElement(_Window.Window, {
    onClose: handleClose,
    disableScrollLock: !handleScroll,
    disableFocusLock: disableFocusLock,
    style: {
      position: windowAbsolutelyPositioned ? "absolute" : "fixed"
    }
  }, /*#__PURE__*/_react.default.createElement(_Overlay.Overlay, {
    className: backgroundElementClass,
    onClick: handleClose,
    tabIndex: -1,
    disableBackgroundColor: true
  }), /*#__PURE__*/_react.default.createElement(_Popper.Popper, {
    anchorEl: childNode,
    open: childNode ? open : false,
    placement: position,
    className: popperClassName,
    tabIndex: -1,
    afterWrite: onPopperOpen
  }, /*#__PURE__*/_react.default.createElement(_Styles.StyledMenu, {
    className: (0, _classnames.default)("c-simple-menu__paper", "c-simple-menu__".concat(mode), menuClassName, className),
    $mode: mode,
    onMouseDown: onMenuClick
  }, /*#__PURE__*/_react.default.createElement(_context.MenuContextProvider, {
    value: {
      open,
      setOpen
    }
  }, typeof children === "function" ? children(childProps) : children)))) : null);
};
Menu.displayName = "Menu";
//# sourceMappingURL=Menu.js.map