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