"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.openEmbedModal = openEmbedModal;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _react = _interopRequireDefault(require("react"));
var _reactDom = _interopRequireDefault(require("react-dom"));
var _reactIntlNext = require("react-intl-next");
var _excluded = ["onClose"];
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); }
var IFRAME_NAME = 'twp-editor-preview-iframe';
var POPUP_MOUNT_POINT_ID = 'twp-editor-preview-iframe';

/*
  Explanatory note:
  Actions don't have access to the react tree of whatever is rendered them
  (and this concept is fraught inside editor anyway) so we want to ensure
  it is mounting to a new unique place. This function manages that, including
  creating an element if it doesn't exist, as well as tidying up the react tree
  (but not the element) upon closing the modal.

  This may strike you as really uncomfortable as you read it, so I wanted to note
  that a) this was discussed and agreed upon, and b) it's definitely odd, and if
  you find an elegant solution around this, you should definitely feel free to
  refactor it.
*/
function openEmbedModal() {
  return _openEmbedModal.apply(this, arguments);
}
function _openEmbedModal() {
  _openEmbedModal = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
    var _ref,
      _ref$onClose,
      _onClose,
      props,
      popupMountPoint,
      Modal,
      _args = arguments;
    return _regenerator.default.wrap(function _callee$(_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          _ref = _args.length > 0 && _args[0] !== undefined ? _args[0] : {};
          _ref$onClose = _ref.onClose, _onClose = _ref$onClose === void 0 ? function () {} : _ref$onClose, props = (0, _objectWithoutProperties2.default)(_ref, _excluded);
          popupMountPoint = document.getElementById(POPUP_MOUNT_POINT_ID);
          if (!popupMountPoint) {
            popupMountPoint = document.createElement('div');
            popupMountPoint.id = POPUP_MOUNT_POINT_ID;
            popupMountPoint.setAttribute('data-testid', 'preview-modal');
            document.body.appendChild(popupMountPoint);
          }
          _context2.next = 6;
          return Promise.resolve().then(function () {
            return _interopRequireWildcard(require('./index'));
          });
        case 6:
          Modal = _context2.sent;
          _reactDom.default.render( /*#__PURE__*/_react.default.createElement(_reactIntlNext.IntlProvider, {
            locale: "en"
          }, /*#__PURE__*/_react.default.createElement(Modal.default, (0, _extends2.default)({}, props, {
            iframeName: IFRAME_NAME,
            onClose: function onClose(_context) {
              if (popupMountPoint) {
                _reactDom.default.unmountComponentAtNode(popupMountPoint);
              }
              if (_onClose) {
                _onClose(_context);
              }
            },
            showModal: true
          }))), popupMountPoint);
        case 8:
        case "end":
          return _context2.stop();
      }
    }, _callee);
  }));
  return _openEmbedModal.apply(this, arguments);
}