"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

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

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

var _modalFunctionComp = _interopRequireDefault(require("./modalFunctionComp"));

var _actionsModal = _interopRequireDefault(require("./actionsModal"));

var _tuple = require("../utils/tuple");

var _destroyFns = _interopRequireDefault(require("./destroyFns"));

var ActionsTuple = (0, _tuple.tuple)('info', 'success', 'warning', 'error');
var modal_global_config = {};

function open(config) {
  var div = document.createElement('div');
  document.body.appendChild(div);
  var currentConfig = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, config), {}, {
    visible: true,
    closeModal: closeModal
  });

  function destoryModal() {
    var unmountResult = _reactDom.default.unmountComponentAtNode(div);

    if (unmountResult && div.parentNode) {
      div.parentNode.removeChild(div);
    }
  }

  if (!_destroyFns.default.has(destoryModal)) {
    _destroyFns.default.add(destoryModal);
  }

  function renderModal(modalConfig) {
    var endModalConfig = (0, _objectSpread2.default)({
      getContainer: modal_global_config.getContainer
    }, modalConfig);
    setTimeout(function () {
      _reactDom.default.render( /*#__PURE__*/_react.default.createElement(_actionsModal.default, Object.assign({}, endModalConfig)), div);
    });
  }

  function closeModal() {
    currentConfig = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, currentConfig), {}, {
      visible: false,
      afterClose: function afterClose() {
        if (typeof config.afterClose === 'function') {
          config.afterClose();
        }

        destoryModal();

        if (_destroyFns.default.has(destoryModal)) {
          _destroyFns.default.delete(destoryModal);
        }
      }
    });
    renderModal(currentConfig);
  }

  function update(updateConfig) {
    if (typeof updateConfig === 'function') {
      currentConfig = updateConfig(currentConfig);
    } else {
      currentConfig = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, currentConfig), updateConfig);
    }

    renderModal(currentConfig);
  }

  renderModal(currentConfig);
  return {
    destroy: closeModal,
    update: update
  };
}

var Modal = _modalFunctionComp.default;

Modal.config = function (globalProps) {
  modal_global_config.getContainer = globalProps.getContainer;
};

Modal.destroyAll = function () {
  _destroyFns.default.forEach(function (fn) {
    return fn === null || fn === void 0 ? void 0 : fn();
  });

  _destroyFns.default.clear();
};

ActionsTuple.forEach(function (action) {
  Modal[action] = function (config) {
    return open((0, _objectSpread2.default)({
      type: action
    }, config));
  };
});
var _default = Modal;
exports.default = _default;