"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const types = require("../../../utils/types.js");
const shared_esmBundler = require("../../../../../@vue/shared/dist/shared.esm-bundler.js");
const error = require("../../../utils/error.js");
const index$1 = require("./index.js");
const Vue = require("vue");
const index = require("../../../../node_modules/@vueuse/shared/index.js");
const messageInstance = /* @__PURE__ */ new Map();
const getAppendToElement = (props) => {
  let appendTo = document.body;
  if (props.appendTo) {
    if (shared_esmBundler.isString(props.appendTo)) appendTo = document.querySelector(props.appendTo);
    if (types.isElement(props.appendTo)) appendTo = props.appendTo;
    if (!types.isElement(appendTo)) {
      error.debugWarn("ElMessageBox", "the appendTo option is not an HTMLElement. Falling back to document.body.");
      appendTo = document.body;
    }
  }
  return appendTo;
};
const initInstance = (props, container, appContext = null) => {
  const vnode = Vue.createVNode(index$1.default, props, shared_esmBundler.isFunction(props.message) || Vue.isVNode(props.message) ? { default: shared_esmBundler.isFunction(props.message) ? props.message : () => props.message } : null);
  vnode.appContext = appContext;
  Vue.render(vnode, container);
  getAppendToElement(props).appendChild(container.firstElementChild);
  return vnode.component;
};
const genContainer = () => {
  return document.createElement("div");
};
const showMessage = (options, appContext) => {
  const container = genContainer();
  options.onVanish = () => {
    Vue.render(null, container);
    messageInstance.delete(vm);
  };
  options.onAction = (action) => {
    const currentMsg = messageInstance.get(vm);
    let resolve;
    if (options.showInput) resolve = {
      value: vm.inputValue,
      action
    };
    else resolve = action;
    if (options.callback) options.callback(resolve, instance.proxy);
    else if (action === "cancel" || action === "close") if (options.distinguishCancelAndClose && action !== "cancel") currentMsg.reject("close");
    else currentMsg.reject("cancel");
    else currentMsg.resolve(resolve);
  };
  const instance = initInstance(options, container, appContext);
  const vm = instance.proxy;
  for (const prop in options) if (shared_esmBundler.hasOwn(options, prop) && !shared_esmBundler.hasOwn(vm.$props, prop)) if (prop === "closeIcon" && shared_esmBundler.isObject(options[prop])) vm[prop] = Vue.markRaw(options[prop]);
  else vm[prop] = options[prop];
  vm.visible = true;
  return vm;
};
function MessageBox(options, appContext = null) {
  if (!index.isClient) return Promise.reject();
  let callback;
  if (shared_esmBundler.isString(options) || Vue.isVNode(options)) options = { message: options };
  else callback = options.callback;
  return new Promise((resolve, reject) => {
    const vm = showMessage(options, appContext ?? MessageBox._context);
    messageInstance.set(vm, {
      options,
      callback,
      resolve,
      reject
    });
  });
}
const MESSAGE_BOX_VARIANTS = [
  "alert",
  "confirm",
  "prompt"
];
const MESSAGE_BOX_DEFAULT_OPTS = {
  alert: {
    closeOnPressEscape: false,
    closeOnClickModal: false
  },
  confirm: { showCancelButton: true },
  prompt: {
    showCancelButton: true,
    showInput: true
  }
};
MESSAGE_BOX_VARIANTS.forEach((boxType) => {
  MessageBox[boxType] = messageBoxFactory(boxType);
});
function messageBoxFactory(boxType) {
  return (message, title, options, appContext) => {
    let titleOrOpts = "";
    if (shared_esmBundler.isObject(title)) {
      options = title;
      titleOrOpts = "";
    } else if (types.isUndefined(title)) titleOrOpts = "";
    else titleOrOpts = title;
    return MessageBox(Object.assign({
      title: titleOrOpts,
      message,
      type: "",
      ...MESSAGE_BOX_DEFAULT_OPTS[boxType]
    }, options, { boxType }), appContext);
  };
}
MessageBox.close = () => {
  messageInstance.forEach((_, vm) => {
    vm.doClose();
  });
  messageInstance.clear();
};
MessageBox._context = null;
exports.default = MessageBox;
