import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
var _excluded = ["onClose"];
import _regeneratorRuntime from "@babel/runtime/regenerator";
import React from 'react';
import ReactDOM from 'react-dom';
import { IntlProvider } from 'react-intl';
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.
*/
export function openEmbedModal() {
  return _openEmbedModal.apply(this, arguments);
}
function _openEmbedModal() {
  _openEmbedModal = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var _ref,
      _ref$onClose,
      _onClose,
      props,
      popupMountPoint,
      Modal,
      _args = arguments;
    return _regeneratorRuntime.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 = _objectWithoutProperties(_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 import('./index');
        case 6:
          Modal = _context2.sent;
          ReactDOM.render( /*#__PURE__*/React.createElement(IntlProvider, {
            locale: "en"
          }, /*#__PURE__*/React.createElement(Modal.default, _extends({}, props, {
            iframeName: IFRAME_NAME,
            onClose: function onClose(_context) {
              if (popupMountPoint) {
                ReactDOM.unmountComponentAtNode(popupMountPoint);
              }
              if (_onClose) {
                _onClose(_context);
              }
            },
            showModal: true
          }))), popupMountPoint);
        case 8:
        case "end":
          return _context2.stop();
      }
    }, _callee);
  }));
  return _openEmbedModal.apply(this, arguments);
}