'use client';
import{useRef as e,useState as n,useCallback as r,useEffect as t}from"react";function useInView({triggerOnce:i,skip:o,addRootMargin:c=0,initialInView:u,onChange:w}={}){let d=e(),[m,v]=n({inView:!!u}),l=e(),s=r(e=>{if(void 0!==d.current&&(d.current(),d.current=void 0,l.current=void 0),o||!e)return;let n=()=>{l.current||(l.current=requestAnimationFrame(()=>{let r=e.getBoundingClientRect(),t=-c,o=(window.innerHeight||document.documentElement.clientHeight)+c,u=r.top<=t&&r.bottom>=t||r.top>=t&&r.bottom<=o||r.top<=o&&r.bottom>=o;u!==m.inView&&(v({inView:u}),w&&w(u,e),u&&i&&d.current&&(window.removeEventListener("scroll",n),window.removeEventListener("resize",n),l.current&&cancelAnimationFrame(l.current),d.current=void 0,l.current=void 0)),l.current=void 0}))};window.addEventListener("scroll",n),window.addEventListener("resize",n),n(),d.current=()=>{window.removeEventListener("scroll",n),window.removeEventListener("resize",n),l.current&&cancelAnimationFrame(l.current),l.current=void 0}},[o,c,m.inView,w,i]);t(()=>{d.current||!m.entry||i||o||v({inView:!!u})},[m.entry,i,o,u]);let a=[s,m.inView,m.entry];return a.ref=a[0],a.inView=a[1],a.entry=a[2],a}export{useInView};
