function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

/**
 * @class qrenderer.event.MultiDragDrop
 * 支持同时拖拽多个元素，按住 Ctrl 键可以多选。
 * 
 * @author 大漠穷秋 <damoqiongqiu@126.com>
 * @docauthor 大漠穷秋 <damoqiongqiu@126.com>
 */
var MultiDragDrop =
/*#__PURE__*/
function () {
  /**
   * @method constructor MultiDragDrop
   * @param {QRendererEventHandler} handler 
   */
  function MultiDragDrop(handler) {
    _classCallCheck(this, MultiDragDrop);

    this.selectionMap = new Map();
    this.handler = handler;
    this.handler.on('mousedown', this._dragStart, this);
  }
  /**
   * @private
   * @method param
   * @param {Element} target 
   * @param {Event} e 
   */


  _createClass(MultiDragDrop, [{
    key: "param",
    value: function param(target, e) {
      return {
        target: target,
        topTarget: e && e.topTarget
      };
    }
    /**
     * @method getSelectedItems
     * 获取当前选中的元素
     * @return {Map} selectionMap
     */

  }, {
    key: "getSelectedItems",
    value: function getSelectedItems() {
      return this.selectionMap;
    }
    /**
     * @method clearSelectionMap
     * 清除选中
     */

  }, {
    key: "clearSelectionMap",
    value: function clearSelectionMap() {
      this.selectionMap.forEach(function (el, key) {
        el.dragging = false;
      });
      this.selectionMap.clear();
    }
    /**
     * @private
     * @method _dragStart
     * 开始拖动
     * @param {Event} e 
     */

  }, {
    key: "_dragStart",
    value: function _dragStart(e) {
      var _this = this;

      var el = e.target;
      var event = e.event;
      this._draggingItem = el;

      if (!el) {
        this.clearSelectionMap();
        return;
      }

      if (!el.draggable) {
        return;
      }

      if (!event.ctrlKey && !this.selectionMap.get(el.id)) {
        this.clearSelectionMap();
      }

      el.dragging = true;
      this.selectionMap.set(el.id, el);
      this._x = e.offsetX;
      this._y = e.offsetY;
      this.handler.on('pagemousemove', this._drag, this);
      this.handler.on('pagemouseup', this._dragEnd, this);
      this.selectionMap.forEach(function (el, key) {
        console.log(el);

        _this.handler.dispatchToElement(_this.param(el, e), 'dragstart', e.event);
      });
    }
    /**
     * @private
     * @method _drag
     * 拖动过程中
     * @param {Event} e 
     */

  }, {
    key: "_drag",
    value: function _drag(e) {
      var _this2 = this;

      var x = e.offsetX;
      var y = e.offsetY;
      var dx = x - this._x;
      var dy = y - this._y;
      this._x = x;
      this._y = y;
      this.selectionMap.forEach(function (el, key) {
        el.drift(dx, dy, e);

        _this2.handler.dispatchToElement(_this2.param(el, e), 'drag', e.event);
      });
      var dropTarget = this.handler.findHover(x, y, this._draggingItem).target;
      var lastDropTarget = this._dropTarget;
      this._dropTarget = dropTarget;

      if (this._draggingItem !== dropTarget) {
        if (lastDropTarget && dropTarget !== lastDropTarget) {
          this.handler.dispatchToElement(this.param(lastDropTarget, e), 'dragleave', e.event);
        }

        if (dropTarget && dropTarget !== lastDropTarget) {
          this.handler.dispatchToElement(this.param(dropTarget, e), 'dragenter', e.event);
        }
      }
    }
    /**
     * @private
     * @method _dragEnd
     * 拖动结束
     * @param {Event} e 
     */

  }, {
    key: "_dragEnd",
    value: function _dragEnd(e) {
      var _this3 = this;

      this.selectionMap.forEach(function (el, key) {
        el.dragging = false;

        _this3.handler.dispatchToElement(_this3.param(el, e), 'dragend', e.event);
      });
      this.handler.off('pagemousemove', this._drag);
      this.handler.off('pagemouseup', this._dragEnd);

      if (this._dropTarget) {
        this.handler.dispatchToElement(this.param(this._dropTarget, e), 'drop', e.event);
      }

      this._dropTarget = null;
    }
  }]);

  return MultiDragDrop;
}();

module.exports = MultiDragDrop;