function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (it) return (it = it.call(o)).next.bind(it); if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
import { makeAutoObservable } from 'mobx';
import ComponentSpec from './componentSpec';
import { innerMaterial } from '../shell';
import { Dragon, isDragDataNode } from './dragon';
import Host from './host';
import Detection from './detection';
import Viewport from './viewport';
var Designer = function Designer(project) {
  var _this = this;
  this.componentSpecMap = new Map();
  this.componentImplMap = new Map();
  this.dragon = new Dragon(this);
  this.host = void 0;
  this.project = void 0;
  this.viewport = void 0;
  this.detection = new Detection(this);
  this.settingTopEntry = void 0;
  this.buildComponentSpecMap = function (packageNames) {
    packageNames.forEach(function (packageName) {
      var result = innerMaterial.getComponentSpecRaw(packageName);
      if (result) {
        _this.componentSpecMap.set(packageName, new ComponentSpec(result));
      }
    });
  };
  this.addComponentsImpl = function (componentMap) {
    for (var _iterator = _createForOfIteratorHelperLoose(componentMap), _step; !(_step = _iterator()).done;) {
      var _step$value = _step.value,
        name = _step$value[0],
        component = _step$value[1];
      _this.addComponentImpl(name, component);
    }
  };
  this.addComponentImpl = function (name, component) {
    if (_this.componentImplMap.has(name)) {
      console.error(name + " \u7684\u5B9E\u73B0\u5DF2\u7ECF\u5B58\u5728\uFF0C\u5373\u5C06\u91CD\u7F6E\uFF01\uFF01\uFF01");
    }
    _this.componentImplMap.set(name, component);
  };
  this.mountViewport = function (element) {
    _this.viewport.mount(element);
  };
  this.getDropContainer = function (locateEvent) {
    var containerNode = _this.host.getClosestNodeByLocation({
      clientX: locateEvent.clientX,
      clientY: locateEvent.clientY
    });
    var thisComponentSpec = isDragDataNode(locateEvent.dragObject) ? locateEvent.dragObject.data : locateEvent.dragObject.node.componentSpec;
    while (containerNode) {
      if (containerNode.componentSpec.isCanInclude(thisComponentSpec)) {
        return containerNode;
      } else {
        containerNode = containerNode.parent;
      }
    }
  };
  this.getNodeRect = function (nodeId) {
    return _this.host.getNodeRect(nodeId);
  };
  this.getInsertPointRect = function () {
    var dropLocation = _this.dragon.dropLocation;
    if (dropLocation) {
      var containerNode = dropLocation.containerNode,
        index = dropLocation.index;
      if (index === 0) {
        return _this.getNodeRect(containerNode.id);
      }
      // 插到容器的最后一个位置
      else if (index >= containerNode.childrenSize) {
        var lastChild = containerNode.lastChild;
        var lastChildRect = lastChild ? _this.getNodeRect(lastChild.id) : undefined;
        return lastChildRect ? new DOMRect(lastChildRect.x, lastChildRect.y + lastChildRect.height, lastChildRect.width, lastChildRect.height) : undefined;
      } else {
        var child = containerNode.getChildAtIndex(index);
        return child ? _this.getNodeRect(child.id) : undefined;
      }
    }
  };
  this.selectNode = function (nodeId) {
    var _this$project$documen;
    _this.project.documentModel.selectNode(nodeId);
    _this.detection.computeSelectedPosition(nodeId);
    _this.settingTopEntry = nodeId ? (_this$project$documen = _this.project.documentModel.getNode(nodeId)) === null || _this$project$documen === void 0 ? void 0 : _this$project$documen.settingEntry : undefined;
  };
  this.rerender = function () {
    return _this.host.rerender();
  };
  makeAutoObservable(this, {
    dragon: false,
    project: false,
    viewport: false,
    host: false,
    componentImplMap: false
  });
  this.project = project;
  this.host = new Host(project);
  this.viewport = new Viewport();
};
export { Designer as default };