'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
require('../../../utils/index.cjs');
var index = require('../../config-provider/index.cjs');
var config = require('../../config-provider/src/config.cjs');
var ConfirmDialog = require('./ConfirmDialog.cjs');
var locale = require('./locale.cjs');
var destroyFns = require('./destroyFns.cjs');
var omit = require('../../../utils/lang/omit.cjs');
var vnode = require('../../../utils/vue/vnode.cjs');

function confirm(config$1) {
  const container = document.createDocumentFragment();
  let currentConfig = {
    ...omit.omit(config$1, ["parentContext", "appContext"]),
    close,
    open: true
  };
  let confirmDialogInstance = null;
  function destroy(...args) {
    if (confirmDialogInstance) {
      vue.render(null, container);
      confirmDialogInstance = null;
    }
    const triggerCancel = args.some((param) => param && param.triggerCancel);
    if (config$1.onCancel && triggerCancel)
      config$1.onCancel(() => {
      }, ...args.slice(1));
    for (let i = 0; i < destroyFns.default.length; i++) {
      const fn = destroyFns.default[i];
      if (fn === close) {
        destroyFns.default.splice(i, 1);
        break;
      }
    }
  }
  function close(...args) {
    currentConfig = {
      ...currentConfig,
      open: false,
      afterClose: () => {
        if (typeof config$1.afterClose === "function")
          config$1.afterClose();
        destroy.apply(this, args);
      }
    };
    if (currentConfig.visible)
      delete currentConfig.visible;
    update(currentConfig);
  }
  function update(configUpdate) {
    if (typeof configUpdate === "function") {
      currentConfig = configUpdate(currentConfig);
    } else {
      currentConfig = {
        ...currentConfig,
        ...configUpdate
      };
    }
    if (confirmDialogInstance)
      vnode.triggerVNodeUpdate(confirmDialogInstance, currentConfig, container);
  }
  const Wrapper = (p) => {
    const global = config.globalConfigForApi;
    const rootPrefixCls = global.prefixCls;
    const prefixCls = p.prefixCls || `${rootPrefixCls}-modal`;
    const iconPrefixCls = global.iconPrefixCls;
    const runtimeLocale = locale.getConfirmLocale();
    return vue.createVNode(index.ConfigProvider, {
      ...global,
      "prefixCls": rootPrefixCls
    }, {
      default: () => [vue.createVNode(ConfirmDialog.default, {
        ...p,
        "rootPrefixCls": rootPrefixCls,
        "prefixCls": prefixCls,
        "iconPrefixCls": iconPrefixCls,
        "locale": runtimeLocale,
        "cancelText": p.cancelText || runtimeLocale.cancelText
      }, null)]
    });
  };
  function render(props) {
    const vm = vue.createVNode(Wrapper, {
      ...props
    });
    vm.appContext = config$1.parentContext || config$1.appContext || vm.appContext;
    vue.render(vm, container);
    return vm;
  }
  confirmDialogInstance = render(currentConfig);
  destroyFns.default.push(close);
  return {
    destroy: close,
    update
  };
}
function withWarn(props) {
  return {
    ...props,
    type: "warning"
  };
}
function withInfo(props) {
  return {
    ...props,
    type: "info"
  };
}
function withSuccess(props) {
  return {
    ...props,
    type: "success"
  };
}
function withError(props) {
  return {
    ...props,
    type: "error"
  };
}
function withConfirm(props) {
  return {
    ...props,
    type: "confirm"
  };
}

exports.default = confirm;
exports.withConfirm = withConfirm;
exports.withError = withError;
exports.withInfo = withInfo;
exports.withSuccess = withSuccess;
exports.withWarn = withWarn;
//# sourceMappingURL=confirm.cjs.map
