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;i=_iterator.length)break;_ref=_iterator[_i++];}else{_i=_iterator.next();if(_i.done)break;_ref=_i.value;}var _link=_ref;var child=this.getNode(_link.to);if(child.depth!==node.depth+1){if(child.depth){this.levels[child.depth].delete(child.id);}child.depth=node.depth+1;if(!this.levels[child.depth]){this.levels[child.depth]=new Set();}this.levels[child.depth].add(child.id);this.updateLevels(child);}}}},{key:"insert",value:function insert(node,parentNode){if(this.nodes[node.id]){throw new Error("Node already Exists");}if(parentNode===null||parentNode===undefined){(0,_get2.default)((0,_getPrototypeOf2.default)(Tree.prototype),"addNode",this).call(this,node);this.root=node;this.root.depth=0;this.levels=[new Set([this.root.id])];return;}if(!this.nodes[parentNode.id]){throw new Error("Parent Node does not Exist....Insert First ");}var link=new Link(parentNode.id,node.id);(0,_get2.default)((0,_getPrototypeOf2.default)(Tree.prototype),"addNode",this).call(this,node);(0,_get2.default)((0,_getPrototypeOf2.default)(Tree.prototype),"addLink",this).call(this,link);this.updateLevels(parentNode);}},{key:"print",value:function print(){console.log("§§§§§§§ Tree Structure §§§§§§§§§§");this.levels.forEach(function(level,index){console.log("Level ",index);level.forEach(function(id){return console.log(" "+id+" ");});});console.log("§§§§§§§ Tree Structure End §§§§§§§§§§");}},{key:"remove",value:function remove(node){var propagation=arguments.length>1&&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;for(var _iterator2=OldLinks.in,_isArray2=Array.isArray(_iterator2),_i2=0,_iterator2=_isArray2?_iterator2:_iterator2[typeof Symbol==="function"?Symbol.iterator:"@@iterator"]();;){var _ref2;if(_isArray2){if(_i2>=_iterator2.length)break;_ref2=_iterator2[_i2++];}else{_i2=_iterator2.next();if(_i2.done)break;_ref2=_i2.value;}var _m=_ref2;OldFromNode=_m.from;}for(var _iterator3=OldLinks.out,_isArray3=Array.isArray(_iterator3),_i3=0,_iterator3=_isArray3?_iterator3:_iterator3[typeof Symbol==="function"?Symbol.iterator:"@@iterator"]();;){var _ref3;if(_isArray3){if(_i3>=_iterator3.length)break;_ref3=_iterator3[_i3++];}else{_i3=_iterator3.next();if(_i3.done)break;_ref3=_i3.value;}var _k=_ref3;OldToNodes.push(_k.to);}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};