const defaultConfiguration={childList:!0,subtree:!0,attributes:!0};class TrackedNode{constructor(i,t){this.node=i,this.tracker=t,this.initialize()}update(i){this.tracker.log("Executing update function.",this.node),this.tracker.updateFunction(this.node)}initialize(){this.tracker.log("Executing initialize function.",this.node),this.tracker.initializeFunction(this.node)}uninitialize(){this.tracker.log("Executing uninitialize function.",this.node),this.tracker.uninitializeFunction(this.node)}reinitialize(){this.uninitialize(),this.initialize()}}class DOMWatcher{constructor(i){i=i||{},this.name=i.name||"Unnamed DOMWatcher",this.debug=i.debug,this.watchedNode=i.node||document.body,this.initializeFunction=i.initializeFunction||function(){},this.uninitializeFunction=i.uninitializeFunction||function(){},this.updateFunction=i.updateFunction||function(){},this.filterFunction=i.filterFunction||function(){return!0},this.configuration=i.configuration||defaultConfiguration,this.trackedNodes=[],!1!==i.initialize&&this.initialize(),this.observer=new MutationObserver(this.callback()),this.observer.observe(this.watchedNode,this.configuration)}log(i){this.debug&&console.log.apply(null,[`DOMWatcher "${this.name}": ${i}`].concat(Array.from(arguments).slice(1)))}getTrackedNode(i){return this.trackedNodes.filter((t=>t.node===i)).shift()}recurseNode(i){let t=this.getTrackedNode(i);if(t)t.reinitialize();else{this.filterFunction(i)&&this.addTrackedNode(i);for(let t of i.childNodes)t instanceof HTMLElement&&this.recurseNode(t)}}updateTrackedNode(i,t){this.log("Updating tracked node",i),i.update(t)}addTrackedNode(i){this.log("Adding tracked node",i),this.trackedNodes.push(new TrackedNode(i,this))}removeTrackedNode(i){this.log("Removing tracked node",i);let t=this.trackedNodes.map((i=>i.node)).indexOf(i);this.trackedNodes[t].uninitialize(),this.trackedNodes=this.trackedNodes.slice(0,t).concat(this.trackedNodes.slice(t+1))}attributeChange(i){let t=this.getTrackedNode(i),e=this.filterFunction(i);t&&!e?this.removeTrackedNode(i):!t&&e?this.addTrackedNode(i):t&&e&&this.updateTrackedNode(t,i)}initialize(){this.recurseNode(this.watchedNode)}callback(){return i=this,function(t,e){for(let e of t)switch(e.type){case"childList":for(let t of e.addedNodes)t instanceof HTMLElement&&i.recurseNode(t);break;case"subtree":break;case"attributes":i.attributeChange(e.target)}};var i}}export{DOMWatcher};
