import { render, createVNode } from 'vue';
import '../../../utils/index.mjs';
import { ConfigProvider } from '../../config-provider/index.mjs';
import { globalConfigForApi } from '../../config-provider/src/config.mjs';
import ConfirmDialog from './ConfirmDialog.mjs';
import { getConfirmLocale } from './locale.mjs';
import destroyFns from './destroyFns.mjs';
import { omit } from '../../../utils/lang/omit.mjs';
import { triggerVNodeUpdate } from '../../../utils/vue/vnode.mjs';

function confirm(config) {
  const container = document.createDocumentFragment();
  let currentConfig = {
    ...omit(config, ["parentContext", "appContext"]),
    close,
    open: true
  };
  let confirmDialogInstance = null;
  function destroy(...args) {
    if (confirmDialogInstance) {
      render(null, container);
      confirmDialogInstance = null;
    }
    const triggerCancel = args.some((param) => param && param.triggerCancel);
    if (config.onCancel && triggerCancel)
      config.onCancel(() => {
      }, ...args.slice(1));
    for (let i = 0; i < destroyFns.length; i++) {
      const fn = destroyFns[i];
      if (fn === close) {
        destroyFns.splice(i, 1);
        break;
      }
    }
  }
  function close(...args) {
    currentConfig = {
      ...currentConfig,
      open: false,
      afterClose: () => {
        if (typeof config.afterClose === "function")
          config.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)
      triggerVNodeUpdate(confirmDialogInstance, currentConfig, container);
  }
  const Wrapper = (p) => {
    const global = globalConfigForApi;
    const rootPrefixCls = global.prefixCls;
    const prefixCls = p.prefixCls || `${rootPrefixCls}-modal`;
    const iconPrefixCls = global.iconPrefixCls;
    const runtimeLocale = getConfirmLocale();
    return createVNode(ConfigProvider, {
      ...global,
      "prefixCls": rootPrefixCls
    }, {
      default: () => [createVNode(ConfirmDialog, {
        ...p,
        "rootPrefixCls": rootPrefixCls,
        "prefixCls": prefixCls,
        "iconPrefixCls": iconPrefixCls,
        "locale": runtimeLocale,
        "cancelText": p.cancelText || runtimeLocale.cancelText
      }, null)]
    });
  };
  function render$1(props) {
    const vm = createVNode(Wrapper, {
      ...props
    });
    vm.appContext = config.parentContext || config.appContext || vm.appContext;
    render(vm, container);
    return vm;
  }
  confirmDialogInstance = render$1(currentConfig);
  destroyFns.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"
  };
}

export { confirm as default, withConfirm, withError, withInfo, withSuccess, withWarn };
//# sourceMappingURL=confirm.mjs.map
