"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const index_esm = require("../../../@vxe-ui/core/es/index.esm.js");
const XEUtils = require("xe-utils");
const modal = require("./src/modal.js");
const index = require("../dynamics/index.js");
function handleModal(options) {
  index.checkDynamic();
  return new Promise((resolve) => {
    const opts = Object.assign({}, options);
    if (opts.id && modal.allActiveModals.some((comp) => comp.props.id === opts.id)) {
      resolve("exist");
    } else {
      const _onHide = opts.onHide;
      const modalOpts = Object.assign(opts, {
        key: XEUtils.uniqueId(),
        modelValue: true,
        onHide(params) {
          const modalList = index.dynamicStore.modals;
          if (_onHide) {
            _onHide(params);
          }
          index.dynamicStore.modals = modalList.filter((item) => item.key !== modalOpts.key);
          resolve(params.type);
        }
      });
      index.dynamicStore.modals.push(modalOpts);
    }
  });
}
function getModal(id) {
  return XEUtils.find(modal.allActiveModals, ($modal) => $modal.props.id === id);
}
function closeModal(id) {
  const modals = id ? [getModal(id)] : modal.allActiveModals;
  const restPromises = [];
  modals.forEach(($modal) => {
    if ($modal) {
      restPromises.push($modal.close());
    }
  });
  return Promise.all(restPromises);
}
function handleOpen(defOpts, content, title, options) {
  let opts;
  if (XEUtils.isObject(content)) {
    opts = content;
  } else {
    opts = { content: XEUtils.toValueString(content), title };
  }
  return handleModal(Object.assign(Object.assign(Object.assign({}, defOpts), options), opts));
}
function openModal(options) {
  return handleOpen({
    type: "modal"
  }, options);
}
function openAlert(content, title, options) {
  return handleOpen({
    type: "alert",
    lockScroll: true,
    showHeader: true,
    showFooter: true
  }, content, title, options);
}
function openConfirm(content, title, options) {
  return handleOpen({
    type: "confirm",
    status: "question",
    lockScroll: true,
    showHeader: true,
    showFooter: true
  }, content, title, options);
}
function openMessage(content, options) {
  return handleOpen({
    type: "message",
    mask: false,
    lockView: false,
    lockScroll: false,
    showHeader: false
  }, content, "", options);
}
function openNotification(content, title, options) {
  return handleOpen({
    type: "notification",
    mask: false,
    lockView: false,
    lockScroll: false,
    showHeader: true,
    draggable: false,
    position: "top-right",
    width: 320
  }, content, title, options);
}
const ModalController = {
  get: getModal,
  close: closeModal,
  open: openModal,
  alert: openAlert,
  confirm: openConfirm,
  message: openMessage,
  notification: openNotification
};
const VxeModal = Object.assign(modal.default, {
  install: function(app) {
    app.component(modal.default.name, modal.default);
  }
});
index_esm.VxeUI.modal = ModalController;
index.dynamicApp.use(VxeModal);
index_esm.VxeUI.component(modal.default);
const Modal = VxeModal;
exports.Modal = Modal;
exports.ModalController = ModalController;
exports.VxeModal = VxeModal;
exports.default = VxeModal;
