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