"use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");var _objectWithoutProperties2=_interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));var _defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));var _toConsumableArray2=_interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));var _createClass2=_interopRequireDefault(require("@babel/runtime/helpers/createClass"));var _get2=_interopRequireDefault(require("@babel/runtime/helpers/get"));var _classCallCheck2=_interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));var _possibleConstructorReturn2=_interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));var _getPrototypeOf2=_interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));var _inherits2=_interopRequireDefault(require("@babel/runtime/helpers/inherits"));function ownKeys(object,enumerableOnly){var keys=Object.keys(object);if(Object.getOwnPropertySymbols){var symbols=Object.getOwnPropertySymbols(object);if(enumerableOnly)symbols=symbols.filter(function(sym){return Object.getOwnPropertyDescriptor(object,sym).enumerable;});keys.push.apply(keys,symbols);}return keys;}function _objectSpread(target){for(var i=1;i1&&arguments[1]!==undefined?arguments[1]:true;propagation?this.cleanup(node):null;this.levels[node.depth]["delete"](node.id);if(this.levels[node.depth].size===0){this.levels.length=node.depth;}(0,_get2["default"])((0,_getPrototypeOf2["default"])(Tree.prototype),"removeNode",this).call(this,node);}},{key:"cleanup",value:function cleanup(parent){var _this3=this;var children=this.getChildren(parent);children.forEach(function(child){_this3.remove(child);});}},{key:"getParent",value:function getParent(node){return(0,_toConsumableArray2["default"])(this.links[node.id]["in"]).map(function(link){return link.from;})[0];}},{key:"getChildren",value:function getChildren(node){var _this4=this;return(0,_toConsumableArray2["default"])(this.links[node.id].out).map(function(link){return _this4.getNode(link.to);});}},{key:"getLevel",value:function getLevel(depth){return this.levels[depth];}},{key:"replaceNode",value:function replaceNode(oldNode,newNode){var _this5=this;if(!this.nodes[oldNode.id]){throw new Error("The Node you are trying to replace doesnt exist in the current Tree");}newNode.depth=oldNode.depth;var OldLinks=this.getLinks(oldNode.id);var OldFromNode;var OldToNodes=[];var OldDepth=oldNode.depth;var levels=this.levels;var _iteratorNormalCompletion2=true;var _didIteratorError2=false;var _iteratorError2=undefined;try{for(var _iterator2=OldLinks["in"][typeof Symbol==="function"?Symbol.iterator:"@@iterator"](),_step2;!(_iteratorNormalCompletion2=(_step2=_iterator2.next()).done);_iteratorNormalCompletion2=true){var m=_step2.value;OldFromNode=m.from;}}catch(err){_didIteratorError2=true;_iteratorError2=err;}finally{try{if(!_iteratorNormalCompletion2&&_iterator2["return"]!=null){_iterator2["return"]();}}finally{if(_didIteratorError2){throw _iteratorError2;}}}var _iteratorNormalCompletion3=true;var _didIteratorError3=false;var _iteratorError3=undefined;try{for(var _iterator3=OldLinks.out[typeof Symbol==="function"?Symbol.iterator:"@@iterator"](),_step3;!(_iteratorNormalCompletion3=(_step3=_iterator3.next()).done);_iteratorNormalCompletion3=true){var k=_step3.value;OldToNodes.push(k.to);}}catch(err){_didIteratorError3=true;_iteratorError3=err;}finally{try{if(!_iteratorNormalCompletion3&&_iterator3["return"]!=null){_iterator3["return"]();}}finally{if(_didIteratorError3){throw _iteratorError3;}}}this.remove(oldNode,false);if(this.levels.length===0){this.levels=[new Set([newNode.id])];(0,_get2["default"])((0,_getPrototypeOf2["default"])(Tree.prototype),"addNode",this).call(this,newNode);this.root=newNode;return true;;}this.levels[OldDepth].add(newNode.id);(0,_get2["default"])((0,_getPrototypeOf2["default"])(Tree.prototype),"addNode",this).call(this,newNode);if(OldFromNode){(0,_get2["default"])((0,_getPrototypeOf2["default"])(Tree.prototype),"linkNodes",this).call(this,OldFromNode,newNode.id);}OldToNodes.map(function(node){(0,_get2["default"])((0,_getPrototypeOf2["default"])(Tree.prototype),"linkNodes",_this5).call(_this5,newNode.id,node);});if(oldNode===this.root){console.log("never");this.root=newNode;}}},{key:"export",value:function _export(){var data=(0,_get2["default"])((0,_getPrototypeOf2["default"])(Tree.prototype),"export",this).call(this);var levels=this.levels.map(function(level){return Array.from(level);});return _objectSpread({},data,{levels:levels,rootId:this.root.id});}},{key:"import",value:function _import(data){var nodeType=arguments.length>1&&arguments[1]!==undefined?arguments[1]:TreeNode;var parentNode=arguments.length>2?arguments[2]:undefined;var levels=data.levels,rootId=data.rootId,rest=(0,_objectWithoutProperties2["default"])(data,["levels","rootId"]);(0,_get2["default"])((0,_getPrototypeOf2["default"])(Tree.prototype),"import",this).call(this,data,nodeType);if(!parentNode){this.root=(0,_get2["default"])((0,_getPrototypeOf2["default"])(Tree.prototype),"getNode",this).call(this,rootId);this.levels=levels.map(function(setArray){return new Set(setArray);});}else{this.linkNodes(parentNode,rootId);this.updateLevels(this.getNode(parentNode));}}}]);return Tree;}(Graph);module.exports={Tree:Tree,TreeNode:TreeNode};