"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _mobx = require("mobx");
var _node = _interopRequireDefault(require("../node"));
var DocumentModel = exports["default"] = /*#__PURE__*/function () {
  function DocumentModel(project, schema) {
    this.rootNode = void 0;
    this.nodeMap = new Map();
    this.selectedNodeId = void 0;
    this.hoveredNodeId = void 0;
    this.project = void 0;
    this.lifeCycles = {};
    this.interceptors = {};
    (0, _mobx.makeAutoObservable)(this, {
      project: false
    });
    this.open(schema);
    this.project = project;
    this.interceptors = schema.interceptors;
  }
  var _proto = DocumentModel.prototype;
  _proto.createNode = function createNode(schema, pNode) {
    var node = new _node["default"](this, schema, pNode);
    this.nodeMap.set(node.id, node);
    return node;
  };
  _proto.delNode = function delNode(nodeId) {
    var thisNode = this.nodeMap.get(nodeId);
    if (thisNode) {
      var _thisNode$parent;
      this.nodeMap["delete"](nodeId);
      (_thisNode$parent = thisNode.parent) === null || _thisNode$parent === void 0 ? void 0 : _thisNode$parent.delChild(thisNode);
      this.project.designer.rerender();
    }
  };
  _proto.copyNode = function copyNode(node) {
    var schema = node["export"]();
    delete schema.id;
    return this.createNode(schema, undefined);
  };
  _proto.open = function open(schema) {
    this.lifeCycles = schema.lifeCycles;
    this.rootNode = this.createNode(schema, undefined);
  };
  _proto.getNode = function getNode(id) {
    return this.nodeMap.get(id);
  };
  _proto.selectNode = function selectNode(id) {
    this.selectedNodeId = id;
  };
  _proto.hoverNode = function hoverNode(id) {
    this.hoveredNodeId = id;
  };
  _proto.updateLifeCycles = function updateLifeCycles(name, value) {
    this.lifeCycles[name] = value;
  };
  _proto.updateInterceptors = function updateInterceptors(name, value) {
    if (!this.interceptors) {
      this.interceptors = {};
    }
    this.interceptors[name] = value;
    this.project.designer.rerender();
  };
  (0, _createClass2["default"])(DocumentModel, [{
    key: "schema",
    get: function get() {
      return (0, _extends2["default"])({}, this.rootNode["export"](), {
        lifeCycles: this.lifeCycles,
        interceptors: this.interceptors
      });
    }
  }, {
    key: "currentNode",
    get: function get() {
      return this.selectedNodeId ? this.nodeMap.get(this.selectedNodeId) : undefined;
    }
  }]);
  return DocumentModel;
}();