"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.VxeDrawer = exports.DrawerController = exports.Drawer = void 0;
var _core = require("@vxe-ui/core");
var _xeUtils = _interopRequireDefault(require("xe-utils"));
var _drawer = _interopRequireWildcard(require("./src/drawer"));
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 handleDrawer(options) {
  // 使用动态组件渲染动态弹框
  (0, _dynamics.checkDynamic)();
  return new Promise(function (resolve) {
    var opts = Object.assign({}, options);
    if (opts.id && _drawer.allActiveDrawers.some(function (comp) {
      return comp.id === opts.id;
    })) {
      resolve('exist');
    } else {
      var events = Object.assign({}, opts.events);
      var drawerOpts = {
        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 !== drawerOpts.key;
            });
            resolve(params.type);
          }
        })
      };
      _dynamics.dynamicStore.drawers.push(drawerOpts);
    }
  });
}
function getDrawer(id) {
  return _xeUtils.default.find(_drawer.allActiveDrawers, function ($drawer) {
    return $drawer.id === id;
  });
}
/**
 * 全局关闭动态的活动窗口（只能用于关闭动态的创建的活动窗口）
 * 如果传 id 则关闭指定的窗口
 * 如果不传则关闭所有窗口
 */
function closeDrawer(id) {
  var drawers = id ? [getDrawer(id)] : _drawer.allActiveDrawers;
  var restPromises = [];
  drawers.forEach(function ($drawer) {
    if ($drawer) {
      restPromises.push($drawer.close());
    }
  });
  return Promise.all(restPromises);
}
function openDrawer(options) {
  return handleDrawer(Object.assign({}, options));
}
var DrawerController = exports.DrawerController = {
  get: getDrawer,
  close: closeDrawer,
  open: openDrawer
};
var VxeDrawer = exports.VxeDrawer = Object.assign(_drawer.default, {
  install: function install(app) {
    app.component(_drawer.default.name, _drawer.default);
  }
});
_core.VxeUI.drawer = DrawerController;
_dynamics.dynamicApp.use(VxeDrawer);
_core.VxeUI.component(_drawer.default);
var Drawer = exports.Drawer = VxeDrawer;
var _default = exports.default = VxeDrawer;