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