"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _mobx = require("mobx");

var _dataset = require("choerodon-ui/dataset");

var _util = require("../util");

var KeyGen = /*#__PURE__*/_regenerator["default"].mark(function _callee(id) {
  return _regenerator["default"].wrap(function _callee$(_context) {
    while (1) {
      switch (_context.prev = _context.next) {
        case 0:
          if (!true) {
            _context.next = 6;
            break;
          }

          _context.next = 3;
          return "modal-".concat(id);

        case 3:
          id += 1;
          _context.next = 0;
          break;

        case 6:
        case "end":
          return _context.stop();
      }
    }
  }, _callee);
})(1);

var containerInstances = _mobx.observable.array();

var removeInstance = (0, _mobx.action)(function (instance) {
  var index = containerInstances.indexOf(instance);

  if (index > -1) {
    containerInstances.splice(index, 1);
  }
});
var addInstance = (0, _mobx.action)(function (instance) {
  removeInstance(instance);
  containerInstances.unshift(instance);
});

function getKey() {
  return KeyGen.next().value;
}

function clear(closeByLocationChange) {
  containerInstances.forEach(function (instance) {
    instance.clear(closeByLocationChange);
  });
}

var mousePositionEventBound = new WeakSet();

function registerMousePosition() {
  var doc = typeof window === 'undefined' ? undefined : document;

  if (doc && !mousePositionEventBound.has(doc)) {
    // 只有点击事件支持从鼠标位置动画展开
    new _dataset.EventManager(doc).addEventListener('click', function (e) {
      ModalManager.mousePosition = (0, _util.getMousePosition)((0, _util.transformZoomData)(e.clientX), (0, _util.transformZoomData)(e.clientY), window); // 100ms 内发生过点击事件，则从点击位置动画展示
      // 否则直接 zoom 展示
      // 这样可以兼容非点击方式展开

      setTimeout(function () {
        return delete ModalManager.mousePosition;
      }, 100);
    }, true);
    mousePositionEventBound.add(doc);
  }
}

var ModalManager = {
  addInstance: addInstance,
  removeInstance: removeInstance,
  getKey: getKey,
  mousePositionEventBound: mousePositionEventBound,
  containerInstances: containerInstances,
  clear: clear,
  containerStyles: new WeakMap(),
  registerMousePosition: registerMousePosition
};
var _default = ModalManager;
exports["default"] = _default;
//# sourceMappingURL=index.js.map
