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