"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "useModal", {
  enumerable: true,
  get: function get() {
    return _useModal.useModal;
  }
});
exports.Modal = exports.ModalRemove = exports.ModalContent = exports.ModalHeader = exports.ModalBackground = void 0;

var _react = _interopRequireWildcard(require("react"));

var _reactDom = _interopRequireDefault(require("react-dom"));

var _styledComponents = _interopRequireDefault(require("styled-components"));

var _behaviors = require("../../behaviors");

var _patchworkThemeProvider = require("../../../themes/patchwork-theme-provider");

var _primitives = require("../../../primitives");

var _icon = require("../../icon");

var _useFocusTrap = require("./use-focus-trap");

var _modal = require("./modal.css-rules");

var _useModal = require("./use-modal");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var ModalBackground = (0, _styledComponents["default"])(_primitives.Box).withConfig({
  displayName: "ModalBackground",
  componentId: "sc-1jk75sh-0"
})(["display:block;position:fixed;z-index:1;left:0;top:0;width:100%;height:100%;overflow:auto;background-color:", ";"], _modal.getBackgroundColor);
exports.ModalBackground = ModalBackground;
var ModalHeader = (0, _styledComponents["default"])(_primitives.Box).withConfig({
  displayName: "ModalHeader",
  componentId: "sc-1jk75sh-1"
})(["height:", ";position:relative;"], _modal.getModalHeaderPadding);
exports.ModalHeader = ModalHeader;
var ModalContent = (0, _styledComponents["default"])(_primitives.Box).withConfig({
  displayName: "ModalContent",
  componentId: "sc-1jk75sh-2"
})(["max-width:", ";background-color:", ";box-shadow:0 5px 15px 0 ", ";border:solid 1px ", ";border-radius:15px;z-index:2;margin:15vh auto;width:80%;&:focus{border:0;outline:none;}"], _modal.getModalWidth, _modal.getContentBackground, _modal.getContentBoxShadowColor, _modal.getBorderColor);
exports.ModalContent = ModalContent;
var ModalRemove = (0, _styledComponents["default"])(_behaviors.UnstyledButton).withConfig({
  displayName: "ModalRemove",
  componentId: "sc-1jk75sh-3"
})(["padding-top:20px;padding-right:20px;position:absolute;right:0;"]);
exports.ModalRemove = ModalRemove;

function getBody() {
  var documentElement = typeof document !== 'undefined' ? document : undefined;
  return documentElement ? documentElement.body : null;
}

function lockBodyOverflow() {
  var body = getBody();

  if (body !== null && body.dataset.initialOverflow === undefined) {
    body.dataset.initialOverflow = body.style.overflow;
    body.style.overflow = 'hidden';
  }
}

function unlockBodyOverflow() {
  var body = getBody();

  if (body !== null && body.dataset.initialOverflow !== undefined) {
    body.style.overflow = body.dataset.initialOverflow;
    delete body.dataset.initialOverflow;
  }
}

var Modal = function Modal(_ref) {
  var show = _ref.show,
      _ref$persistent = _ref.persistent,
      persistent = _ref$persistent === void 0 ? false : _ref$persistent,
      onClose = _ref.onClose,
      _ref$size = _ref.size,
      size = _ref$size === void 0 ? 'lg' : _ref$size,
      children = _ref.children,
      _ref$className = _ref.className,
      className = _ref$className === void 0 ? '' : _ref$className,
      spacing = _ref.spacing,
      _ref$hideHeader = _ref.hideHeader,
      hideHeader = _ref$hideHeader === void 0 ? false : _ref$hideHeader,
      refToRestoreFocus = _ref.refToRestoreFocus;
  var modalRef = (0, _useFocusTrap.useFocusTrap)({
    show: show,
    refToRestoreFocus: refToRestoreFocus
  });
  var body = getBody();
  (0, _react.useEffect)(function () {
    if (body) {
      return function () {
        unlockBodyOverflow();
      };
    }
  }, [body]);
  var handleBackgroundClick = (0, _react.useCallback)(function (event) {
    event.preventDefault();

    if (!persistent) {
      onClose();
    }
  }, [onClose, persistent]);
  var handleContentClick = (0, _react.useCallback)(function (event) {
    event.stopPropagation();
  }, []);
  var handleCloseClick = (0, _react.useCallback)(function (event) {
    event.stopPropagation();
    onClose();
  }, [onClose]);
  var handleKeyPress = (0, _react.useCallback)(function (event) {
    if (show && !persistent && event.code === 'Escape') {
      onClose();
    }
  }, [onClose, show, persistent]);
  (0, _react.useEffect)(function () {
    if (show && !persistent) {
      window.addEventListener('keydown', handleKeyPress);
    }

    return function () {
      window.removeEventListener('keydown', handleKeyPress);
    };
  }, [handleKeyPress, show, persistent]);
  if (body == null) return null;

  if (show) {
    lockBodyOverflow();
  } else {
    unlockBodyOverflow();
  }

  return show && _reactDom["default"].createPortal(_react["default"].createElement(_patchworkThemeProvider.Resets, {
    face: "sans"
  }, _react["default"].createElement(ModalBackground, {
    onClick: handleBackgroundClick
  }, _react["default"].createElement(ModalContent, {
    onClick: handleContentClick,
    size: size,
    className: className,
    ref: modalRef,
    role: "dialog",
    "aria-modal": "true"
  }, !hideHeader && _react["default"].createElement(ModalHeader, {
    stacked: "row",
    size: size
  }, !persistent && _react["default"].createElement(ModalRemove, {
    insetFocus: true,
    onClick: handleCloseClick
  }, _react["default"].createElement(_icon.Remove, {
    size: 12,
    color: "greyscale-dark"
  }))), _react["default"].createElement(_primitives.Box, {
    spacing: spacing || ['ph6', 'pb6']
  }, children)))), body);
};

exports.Modal = Modal;