import { DragObjectType } from '../types';
import { makeAutoObservable } from 'mobx';
export function isDragDataNode(dragObject) {
  return dragObject.type === DragObjectType.NodeData;
}
export function isDragNode(dragObject) {
  return dragObject.type === DragObjectType.Node;
}
export var Dragon = function Dragon(designer) {
  var _this = this;
  this.dragging = false;
  this.dragObject = null;
  this.dropLocation = void 0;
  this.designer = void 0;
  this.onDragStart = function (dragObject) {
    _this.dragging = true;
    _this.dragObject = dragObject;
  };
  this.onDragEnd = function (event) {
    _this.dragging = false;
    _this.dragObject = null;
    _this.dropLocation = undefined;
  };
  this.onDragOver = function (e) {
    if (_this.dragObject) {
      var locateEvent = _this.createLocationEvent(e);
      _this.dropLocation = _this.locate(locateEvent);
    }
  };
  this.locate = function (locateEvent) {
    var container = _this.designer.getDropContainer(locateEvent);
    if (container) {
      var dropLocation = {
        index: 0,
        containerNode: container
      };
      var childrenSize = container.childrenSize,
        lastChild = container.lastChild;
      var clientY = locateEvent.clientY;
      if (lastChild) {
        var lastChildRect = _this.designer.getNodeRect(lastChild.id);
        // 判断是否要插到容器的末尾
        if (lastChildRect && clientY > lastChildRect.bottom) {
          dropLocation.index = childrenSize;
        } else {
          var minDistance = Infinity;
          // 容器中最近的插入点
          var minIndex = 0;
          for (var index = 0; index < childrenSize; index++) {
            var child = container.getChildAtIndex(index);
            var rect = _this.designer.getNodeRect(child.id);
            if (rect && Math.abs(rect.top - clientY) < minDistance) {
              minDistance = Math.abs(rect.top - clientY);
              minIndex = index;
            }
          }
          dropLocation.index = minIndex;
        }
      }
      return dropLocation;
    }
  };
  this.createLocationEvent = function (e) {
    return {
      dragObject: _this.dragObject,
      originalEvent: e,
      clientX: e.clientX,
      clientY: e.clientY
    };
  };
  makeAutoObservable(this, {
    designer: false
  });
  this.designer = designer;
};