"use strict";

exports.__esModule = true;
exports.PortalSidebar = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactPortal = require("react-portal");
var _styled = require("./styled");
var _excluded = ["children", "className", "closeOnEsc", "closeOnOverlayClick", "onClose", "right", "Wrapper"];
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, 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); }
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 ESCAPE_KEY = 27;
var PortalSidebar = exports.PortalSidebar = function PortalSidebar(_ref) {
  var children = _ref.children,
    className = _ref.className,
    _ref$closeOnEsc = _ref.closeOnEsc,
    closeOnEsc = _ref$closeOnEsc === void 0 ? false : _ref$closeOnEsc,
    _ref$closeOnOverlayCl = _ref.closeOnOverlayClick,
    closeOnOverlayClick = _ref$closeOnOverlayCl === void 0 ? false : _ref$closeOnOverlayCl,
    _ref$onClose = _ref.onClose,
    onClose = _ref$onClose === void 0 ? function () {} : _ref$onClose,
    _ref$right = _ref.right,
    right = _ref$right === void 0 ? false : _ref$right,
    _ref$Wrapper = _ref.Wrapper,
    Wrapper = _ref$Wrapper === void 0 ? _react.Fragment : _ref$Wrapper,
    rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  (0, _react.useEffect)(function () {
    var evHandler = function evHandler(event) {
      if (event.keyCode === ESCAPE_KEY && closeOnEsc) {
        onClose();
      }
    };
    document.addEventListener("keydown", evHandler);
    return function () {
      document.removeEventListener("keydown", evHandler);
    };
  }, [closeOnEsc, onClose]);
  var handleOverlayClick = function handleOverlayClick() {
    if (closeOnOverlayClick) {
      onClose();
    }
  };
  return /*#__PURE__*/_react["default"].createElement(_reactPortal.Portal, null, /*#__PURE__*/_react["default"].createElement(_styled.DisabledOverlay, {
    onClick: handleOverlayClick
  }), /*#__PURE__*/_react["default"].createElement(Wrapper, null, /*#__PURE__*/_react["default"].createElement(_styled.PortalSidebox, _extends({
    className: className,
    shadowSide: right,
    side: right ? "right" : "left"
  }, rest), children)));
};