import{__extends}from"tslib";import{h as debounce}from"./helpers-2253b115.js";import{L as ListenerAbstract,a as SingleSubject}from"./listener-aa4bfae3.js";var BalMutationListener=function(t){__extends(i,t);function i(i){var n=t.call(this)||this;n.tags=[];n.mutationObserver=undefined;n.mutationObserverInit={childList:true,subtree:true,attributes:true,characterData:true};n.mutationCallback=function(t){var i=t.some((function(t){return n.tags.includes(t.target.nodeName)}));if(i){return n.notify(undefined)}var e=t.some((function(t){return Array.from(t.removedNodes).some((function(t){return n.tags.includes(t.nodeName)}))}));if(e){return n.notify(undefined)}var r=t.some((function(t){return t.type==="characterData"}));if(r){return n.notify(undefined)}if(n.tags.length===0&&t.length>0){return n.notify(undefined)}};n.tags=(i.tags||[]).map((function(t){return t.toUpperCase()}));n.mutationObserverInit={childList:i.childList===false?false:true,subtree:i.subtree===false?false:true,attributes:i.attributes===false?false:true,characterData:i.characterData===false?false:true};return n}i.prototype.connect=function(i){t.prototype.connect.call(this,i);if(typeof MutationObserver==="undefined"){return}this.destroyMutationObserver();this.mutationObserver=new MutationObserver(this.mutationCallback);this.mutationObserver.observe(i,this.mutationObserverInit)};i.prototype.disconnect=function(){t.prototype.disconnect.call(this);this.destroyMutationObserver()};i.prototype.destroyMutationObserver=function(){if(this.mutationObserver!==undefined){this.mutationObserver.disconnect();this.mutationObserver=undefined}};return i}(ListenerAbstract);var BalMutationSubject=function(t){__extends(i,t);function i(i){var n=t.call(this,(function(t){if(t.mutationObserverActive){t.mutationListener()}}))||this;n.options=i;n.debouncedNotify=debounce((function(){return n.notify()}),50);n.listener=new BalMutationListener(i);return n}i.prototype.attach=function(i){var n=this;var e,r,u;t.prototype.attach.call(this,i);if(this.options.closest){var s=i.el.closest(this.options.closest);if(s){(e=this.listener)===null||e===void 0?void 0:e.connect(s)}}else{(r=this.listener)===null||r===void 0?void 0:r.connect(i.el)}(u=this.listener)===null||u===void 0?void 0:u.add((function(){return n.debouncedNotify()}))};i.prototype.detach=function(){var i;t.prototype.detach.call(this);(i=this.listener)===null||i===void 0?void 0:i.disconnect()};return i}(SingleSubject);function ListenToMutation(t){return function(i,n,e){var r=i.connectedCallback,u=i.disconnectedCallback;i.connectedCallback=function(){if(!this._balMutationSubject){this._balMutationSubject=new BalMutationSubject(t);this._balMutationSubject.attach(this)}return r&&r.call(this)};i.disconnectedCallback=function(){if(this._balMutationSubject){this._balMutationSubject.detach();this._balMutationSubject=undefined}return u&&u.call(this)}}}export{ListenToMutation as L};