"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = confirm;
exports.modalGlobalConfig = modalGlobalConfig;
exports.withConfirm = withConfirm;
exports.withError = withError;
exports.withInfo = withInfo;
exports.withSuccess = withSuccess;
exports.withWarn = withWarn;
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _react = _interopRequireDefault(require("react"));
var _render = require("rc-util/lib/React/render");
var _InfoCircleOutlined = _interopRequireDefault(require("@ant-design/icons/InfoCircleOutlined"));
var _CheckCircleOutlined = _interopRequireDefault(require("@ant-design/icons/CheckCircleOutlined"));
var _CloseCircleOutlined = _interopRequireDefault(require("@ant-design/icons/CloseCircleOutlined"));
var _ExclamationCircleOutlined = _interopRequireDefault(require("@ant-design/icons/ExclamationCircleOutlined"));
var _locale = require("antd/es/modal/locale");
var _configProvider = require("antd/es/config-provider");
var _ConfirmDialog = _interopRequireDefault(require("./ConfirmDialog"));
var _jsxRuntime = require("react/jsx-runtime");
const destroyFns = [];
let defaultRootPrefixCls = '';
function getRootPrefixCls() {
  return defaultRootPrefixCls;
}
function confirm(config) {
  const container = document.createDocumentFragment();
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
  let currentConfig = {
    ...config,
    close,
    open: true
  };
  function destroy(...args) {
    const triggerCancel = args.some(param => param && param.triggerCancel);
    if (config.onCancel && triggerCancel) {
      config.onCancel(...args);
    }
    for (let i = 0; i < destroyFns.length; i++) {
      const fn = destroyFns[i];
      // eslint-disable-next-line @typescript-eslint/no-use-before-define
      if (fn === close) {
        destroyFns.splice(i, 1);
        break;
      }
    }
    (0, _render.unmount)(container);
  }
  function render({
    okText,
    cancelText,
    prefixCls: customizePrefixCls,
    ...props
  }) {
    /**
     * https://github.com/ant-design/ant-design/issues/23623
     *
     * Sync render blocks React event. Let's make this async.
     */
    setTimeout(() => {
      const runtimeLocale = (0, _locale.getConfirmLocale)();
      const {
        getPrefixCls,
        getIconPrefixCls
      } = (0, _configProvider.globalConfig)();
      // because Modal.config  set rootPrefixCls, which is different from other components
      const rootPrefixCls = getPrefixCls(undefined, getRootPrefixCls());
      const prefixCls = customizePrefixCls || `${rootPrefixCls}-modal`;
      const iconPrefixCls = getIconPrefixCls();
      (0, _render.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_ConfirmDialog.default, {
        ...props,
        prefixCls: prefixCls,
        rootPrefixCls: rootPrefixCls,
        iconPrefixCls: iconPrefixCls,
        okText: okText || (props.okCancel ? runtimeLocale.okText : runtimeLocale.justOkText),
        cancelText: cancelText || runtimeLocale.cancelText
      }), container);
    });
  }
  function close(...args) {
    currentConfig = {
      ...currentConfig,
      open: false,
      afterClose: () => {
        if (typeof config.afterClose === 'function') {
          config.afterClose();
        }
        // @ts-ignore
        destroy.apply(this, args);
      }
    };
    render(currentConfig);
  }
  function update(configUpdate) {
    if (typeof configUpdate === 'function') {
      currentConfig = configUpdate(currentConfig);
    } else {
      currentConfig = {
        ...currentConfig,
        ...configUpdate
      };
    }
    render(currentConfig);
  }
  render(currentConfig);
  destroyFns.push(close);
  return {
    destroy: close,
    update
  };
}
function withWarn(props) {
  return {
    icon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_ExclamationCircleOutlined.default, {}),
    okCancel: false,
    ...props,
    type: 'warning'
  };
}
function withInfo(props) {
  return {
    icon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_InfoCircleOutlined.default, {}),
    okCancel: false,
    ...props,
    type: 'info'
  };
}
function withSuccess(props) {
  return {
    icon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_CheckCircleOutlined.default, {}),
    okCancel: false,
    ...props,
    type: 'success'
  };
}
function withError(props) {
  return {
    icon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_CloseCircleOutlined.default, {}),
    okCancel: false,
    ...props,
    type: 'error'
  };
}
function withConfirm(props) {
  return {
    icon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_ExclamationCircleOutlined.default, {}),
    okCancel: true,
    ...props,
    type: 'confirm'
  };
}
function modalGlobalConfig({
  rootPrefixCls
}) {
  defaultRootPrefixCls = rootPrefixCls;
}