class ZeroDepError extends Error{category;source;value;constructor(message="An unexpected error has occurred",category="unknown",source="unknown",value=void 0){super(message),this.category=category,this.source=source,this.value=value,this.name=this.constructor.name,Object.setPrototypeOf(this,new.target.prototype)}}let getType=Object.prototype.toString,compare=(a,b)=>{if(Object.is(a,b))return!0;if(null==a||null==b)return!1;var typeA=Array.isArray(a)?"Array":getType.call(a).slice(8,-1);if(typeA!==(Array.isArray(b)?"Array":getType.call(b).slice(8,-1)))return!1;switch(typeA){case"Array":if(a.length!==b.length)return!1;for(var ndx in a)if(!compare(a[ndx],b[ndx]))return!1;return!0;case"ArrayBuffer":var dvA=new DataView(a),dvB=new DataView(b);return compare(dvA,dvB);case"Boolean":return!!a==!!b;case"DataView":if(a.byteLength!==b.byteLength)return!1;for(let i=0;i!compare(a[key],b[key])):!1;case"Map":return a.size!==b.size?!1:(dvB=[...a.keys()],dvA=[...b.keys()],dvB.length===dvA.length&&!!compare(dvB,dvA)&&(dvB=[...a.values()],dvA=[...b.values()],compare(dvB,dvA)));case"Set":dvB=[...a].sort(),dvA=[...b].sort();return compare(dvB,dvA);case"BigInt64Array":case"BigUint64Array":case"Float32Array":case"Float64Array":case"Int16Array":case"Int32Array":case"Int8Array":case"Uint16Array":case"Uint32Array":case"Uint8Array":case"Uint8ClampedArray":return a.byteLength===b.byteLength?a.every((value,ndx)=>value===b[ndx]):!1;case"BigInt":case"Number":case"Promise":case"String":case"Generator":return!1;default:dvB=new ZeroDepError(`Cannot compare ${typeA} values`);throw dvB.value=a,dvB}},isEqual=(val1,val2)=>{try{return compare(val1,val2)}catch(err){if(err instanceof ZeroDepError)throw err;return!1}},structTreeFactory=json=>{let rootNode=null,tree={getRootNode:()=>rootNode,setRootNode:data=>{if(rootNode)throw new ZeroDepError("Root node already defined");return rootNode={data:data,parent:null,children:null}},addChild:(node,data)=>{node.children=node.children||[];data={data:data,parent:node,children:null};return node.children.push(data),data},addChildren:(node,items)=>{node.children=node.children||[];items=items.map(data=>({data:data,parent:node,children:null}));return node.children.push(...items),items},dfs:comparator=>{if(!rootNode)return null;let match=null,searchDepthFirst=children=>{for(var child of children){if(match)return;if(comparator(child.data))return void(match=child);child.children&&searchDepthFirst(child.children)}};return rootNode.children&&searchDepthFirst(rootNode.children),match},bfs:comparator=>{if(!rootNode)return null;let match=null,searchBreadthFirst=children=>{for(var child of children){if(match)return;if(comparator(child.data))return void(match=child)}for(let child of children)!match&&child.children&&searchBreadthFirst(child.children)};return rootNode.children&&searchBreadthFirst(rootNode.children),match},dfsFilter:comparator=>{if(!rootNode)return[];let matches=[],searchDepthFirst=children=>{for(var child of children)comparator(child.data)&&matches.push(child),child.children&&searchDepthFirst(child.children)};return rootNode.children&&searchDepthFirst(rootNode.children),matches},bfsFilter:comparator=>{if(!rootNode)return[];let matches=[],searchBreadthFirst=children=>{for(var child of children)comparator(child.data)&&matches.push(child);for(let child of children)child.children&&searchBreadthFirst(child.children)};return rootNode.children&&searchBreadthFirst(rootNode.children),matches},deleteNode:node=>{var parentNode;node.parent?((parentNode=node.parent).children=parentNode.children.filter(child=>!isEqual(node.data,child.data)),parentNode.children.length||(parentNode.children=null)):rootNode=null},toJSON:()=>{if(!rootNode)return null;var obj={data:rootNode.data,children:[]};let getChildren=children=>children.map(child=>({data:child.data,children:child.children?getChildren(child.children):void 0}));return obj.children=rootNode.children?getChildren(rootNode.children):void 0,obj}};if(json){let{children=null,data=void 0,...rest}=json;json=tree.setRootNode(data||rest);let populateChildren=(node,children)=>{for(var child of children){let{children,data,...rest}=child;child=tree.addChild(node,data??rest);children&&populateChildren(child,children)}};children&&populateChildren(json,children)}return tree};exports.structTreeFactory=structTreeFactory;