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