"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = confirm;
exports.withWarn = withWarn;
exports.withInfo = withInfo;
exports.withSuccess = withSuccess;
exports.withError = withError;
exports.withConfirm = withConfirm;

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

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

var ReactDOM = _interopRequireWildcard(require("react-dom"));

var _ExclamationCircle = _interopRequireDefault(require("../icon/ExclamationCircle"));

var _CheckCircle = _interopRequireDefault(require("../icon/CheckCircle"));

var _InfoCircle = _interopRequireDefault(require("../icon/InfoCircle"));

var _CloseCircle = _interopRequireDefault(require("../icon/CloseCircle"));

var _locale = require("./locale");

var _Modal = require("./Modal");

var _ConfirmDialog = _interopRequireDefault(require("./ConfirmDialog"));

var __rest = void 0 && (void 0).__rest || function (s, e) {
  var t = {};

  for (var p in s) {
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  }

  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};

function confirm(config) {
  var div = document.createElement('div');
  document.body.appendChild(div); // eslint-disable-next-line no-use-before-define

  var currentConfig = (0, _extends2["default"])((0, _extends2["default"])({}, config), {
    close: close,
    visible: true
  });

  function destroy() {
    var unmountResult = ReactDOM.unmountComponentAtNode(div);

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

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    var triggerCancel = args.some(function (param) {
      return param && param.triggerCancel;
    });

    if (config.onCancel && triggerCancel) {
      config.onCancel.apply(config, args);
    }

    for (var i = 0; i < _Modal.destroyFns.length; i++) {
      var fn = _Modal.destroyFns[i]; // eslint-disable-next-line no-use-before-define

      if (fn === close) {
        _Modal.destroyFns.splice(i, 1);

        break;
      }
    }
  }

  function render(_a) {
    var okText = _a.okText,
        cancelText = _a.cancelText,
        props = __rest(_a, ["okText", "cancelText"]);
    /**
     * https://github.com/ant-design/ant-design/issues/23623
     * Sync render blocks React event. Let's make this async.
     */


    setTimeout(function () {
      var runtimeLocale = (0, _locale.getConfirmLocale)();
      ReactDOM.render( /*#__PURE__*/React.createElement(_ConfirmDialog["default"], (0, _extends2["default"])({}, props, {
        okText: okText || (props.okCancel ? runtimeLocale.okText : runtimeLocale.justOkText),
        cancelText: cancelText || runtimeLocale.cancelText
      })), div);
    });
  }

  function close() {
    for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
      args[_key2] = arguments[_key2];
    }

    currentConfig = (0, _extends2["default"])((0, _extends2["default"])({}, currentConfig), {
      visible: false,
      afterClose: destroy.bind.apply(destroy, [this].concat(args))
    });
    render(currentConfig);
  }

  function update(newConfig) {
    currentConfig = (0, _extends2["default"])((0, _extends2["default"])({}, currentConfig), newConfig);
    render(currentConfig);
  }

  render(currentConfig);

  _Modal.destroyFns.push(close);

  return {
    destroy: close,
    update: update
  };
}

function withWarn(props) {
  return (0, _extends2["default"])({
    type: 'warning',
    icon: /*#__PURE__*/React.createElement(_ExclamationCircle["default"], null),
    okCancel: false
  }, props);
}

function withInfo(props) {
  return (0, _extends2["default"])({
    type: 'info',
    icon: /*#__PURE__*/React.createElement(_InfoCircle["default"], null),
    okCancel: false
  }, props);
}

function withSuccess(props) {
  return (0, _extends2["default"])({
    type: 'success',
    icon: /*#__PURE__*/React.createElement(_CheckCircle["default"], null),
    okCancel: false
  }, props);
}

function withError(props) {
  return (0, _extends2["default"])({
    type: 'error',
    icon: /*#__PURE__*/React.createElement(_CloseCircle["default"], null),
    okCancel: false
  }, props);
}

function withConfirm(props) {
  return (0, _extends2["default"])({
    type: 'confirm',
    icon: /*#__PURE__*/React.createElement(_ExclamationCircle["default"], null),
    okCancel: true
  }, props);
}