"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.VxeModal = exports.ModalController = exports.Modal = void 0;
var _vue = _interopRequireDefault(require("vue"));
var _core = require("@vxe-ui/core");
var _xeUtils = _interopRequireDefault(require("xe-utils"));
var _modal = _interopRequireWildcard(require("./src/modal"));
var _dynamics = require("../dynamics");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function handleModal(options) {
  // 使用动态组件渲染动态弹框
  (0, _dynamics.checkDynamic)();
  return new Promise(function (resolve) {
    var opts = Object.assign({}, options);
    if (opts.id && _modal.allActiveModals.some(function (comp) {
      return comp.id === opts.id;
    })) {
      resolve('exist');
    } else {
      var events = Object.assign({}, opts.events);
      var modalOpts = {
        key: _xeUtils.default.uniqueId(),
        props: Object.assign(opts, {
          value: true
        }),
        on: Object.assign(Object.assign({}, events), {
          hide: function hide(params) {
            var modalList = _dynamics.dynamicStore.modals;
            if (events.hide) {
              events.hide.call(this, params);
            }
            _dynamics.dynamicStore.modals = modalList.filter(function (item) {
              return item.key !== modalOpts.key;
            });
            resolve(params.type);
          }
        })
      };
      _dynamics.dynamicStore.modals.push(modalOpts);
    }
  });
}
function getModal(id) {
  return _xeUtils.default.find(_modal.allActiveModals, function ($modal) {
    return $modal.id === id;
  });
}
/**
 * 全局关闭动态的活动窗口（只能用于关闭动态的创建的活动窗口）
 * 如果传 id 则关闭指定的窗口
 * 如果不传则关闭所有窗口
 */
function closeModal(id) {
  var modals = id ? [getModal(id)] : _modal.allActiveModals;
  var restPromises = [];
  modals.forEach(function ($modal) {
    if ($modal) {
      restPromises.push($modal.close());
    }
  });
  return Promise.all(restPromises);
}
function handleOpen(defOpts, content, title, options) {
  var opts;
  if (_xeUtils.default.isObject(content)) {
    opts = content;
  } else {
    opts = {
      content: _xeUtils.default.toValueString(content),
      title: 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);
}
var ModalController = exports.ModalController = {
  get: getModal,
  close: closeModal,
  open: openModal,
  alert: openAlert,
  confirm: openConfirm,
  message: openMessage,
  notification: openNotification
};
var VxeModal = exports.VxeModal = Object.assign(_modal.default, {
  install: function install(app) {
    app.component(_modal.default.name, _modal.default);
    // 兼容老版本
    if (!_vue.default.prototype.$vxe) {
      _vue.default.prototype.$vxe = {
        modal: ModalController
      };
    } else {
      _vue.default.prototype.$vxe.modal = ModalController;
    }
  }
});
_core.VxeUI.modal = ModalController;
_dynamics.dynamicApp.use(VxeModal);
_core.VxeUI.component(_modal.default);
var Modal = exports.Modal = VxeModal;
var _default = exports.default = VxeModal;