class t{constructor(t){this.observers=[],this.rootVisibility=new Map,this.hasEmittedOutOfView=!1,this.options=t,this.handleVisibilityChange=this.onDocumentVisibilityChange.bind(this)}setup(){const{target:t,enableVisibilityEffect:i,enableTabClose:s}=this.options;if(!i||!t)return;this.cleanup();const e=this.getScrollableAncestors(this.options.host)[0]??null;(e?[e,null]:[null]).forEach((t=>{const i=new IntersectionObserver((i=>{for(const s of i){if(!s.isIntersecting&&this.options.isOpen?.())return this.options.onAnchorOutOfView?.(t),void this.options.emit?.("visibility-change",{visible:!1,reason:"anchor-out-of-view",root:t});if(!this.options.isOpen?.())return;const i=s.intersectionRatio>0;this.rootVisibility.set(t,i);const e=[...this.rootVisibility.values()],n=e.length>0&&e.some((t=>!1===t));n&&!this.hasEmittedOutOfView&&(this.hasEmittedOutOfView=!0,this.options.onAnchorOutOfView?.(t),this.options.emit?.("visibility-change",{visible:!1,reason:"anchor-out-of-view",root:t})),n||(this.hasEmittedOutOfView=!1)}}),{root:t,threshold:.1});i.observe(this.options.host),this.observers.push(i)})),s&&document.addEventListener("visibilitychange",this.handleVisibilityChange)}cleanup(){this.observers.forEach((t=>t.disconnect())),this.observers=[],this.rootVisibility.clear(),document.removeEventListener("visibilitychange",this.handleVisibilityChange)}onDocumentVisibilityChange(){const{enableTabClose:t,isOpen:i,onDocumentHidden:s,emit:e}=this.options;t&&"hidden"===document.visibilityState&&i?.()&&(s?.(),e?.("visibility-change",{visible:!1,reason:"document-hidden"}))}getScrollableAncestors(t){const i=[];let s=t.parentElement;for(;s;){const t=getComputedStyle(s);/(auto|scroll)/.test(t.overflow+t.overflowY+t.overflowX)&&i.push(s),s=s.parentElement}return i}}export{t as V};
