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 BalVisibilityListener=function(t){__extends(i,t);function i(){var i=t.apply(this,arguments)||this;i.waitAfterFramePrint=false;i.intersectionObserver=undefined;i.intersectionCallback=function(t){var n=t.some((function(t){return t.isIntersecting}));if(n){i.notify()}};return i}i.prototype.connect=function(i){return __awaiter(this,void 0,void 0,(function(){return __generator(this,(function(n){switch(n.label){case 0:t.prototype.connect.call(this,i);if(typeof IntersectionObserver==="undefined"){return[2]}if(!this.waitAfterFramePrint)return[3,3];return[4,deepReady(i)];case 1:n.sent();return[4,waitAfterFramePaint()];case 2:n.sent();n.label=3;case 3:this.destroyMutationObserver();this.intersectionObserver=new IntersectionObserver(this.intersectionCallback,{root:null,threshold:.5});this.intersectionObserver.observe(i);return[2]}}))}))};i.prototype.disconnect=function(){t.prototype.disconnect.call(this);this.destroyMutationObserver()};i.prototype.destroyMutationObserver=function(){if(this.intersectionObserver!==undefined){this.intersectionObserver.disconnect();this.intersectionObserver=undefined}};return i}(ListenerAbstract);var BalVisibilitySubject=function(t){__extends(i,t);function i(){var i=t.call(this,(function(t){t.visibilityListener()}))||this;i.debouncedNotify=debounce((function(){return i.notify()}),50);i.listener=new BalVisibilityListener;return i}i.prototype.attach=function(i){var n=this;var e,r;t.prototype.attach.call(this,i);(e=this.listener)===null||e===void 0?void 0:e.connect(i.el);(r=this.listener)===null||r===void 0?void 0:r.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 ListenToVisibility(){return function(t,i,n){var e=t.connectedCallback,r=t.componentDidLoad,s=t.disconnectedCallback;t.connectedCallback=function(){if(!this._balVisibilitySubject){this._balVisibilitySubject=new BalVisibilitySubject}return e&&e.call(this)};t.componentDidLoad=function(){this._balVisibilitySubject.attach(this);return r&&r.call(this)};t.disconnectedCallback=function(){if(this._balVisibilitySubject){this._balVisibilitySubject.detach();this._balVisibilitySubject=undefined}return s&&s.call(this)}}}export{ListenToVisibility as L};