var _excluded = ["children", "className", "closeOnEsc", "closeOnOverlayClick", "onClose", "right", "Wrapper"];
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; }
import React, { Fragment, useEffect } from "react";
import { Portal } from "react-portal";
import { PortalSidebox, DisabledOverlay } from "./styled";
var ESCAPE_KEY = 27;
export var 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 ? Fragment : _ref$Wrapper,
    rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  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.createElement(Portal, null, /*#__PURE__*/React.createElement(DisabledOverlay, {
    onClick: handleOverlayClick
  }), /*#__PURE__*/React.createElement(Wrapper, null, /*#__PURE__*/React.createElement(PortalSidebox, _extends({
    className: className,
    shadowSide: right,
    side: right ? "right" : "left"
  }, rest), children)));
};