import{useCallback as c,useEffect as d,useRef as w}from"react";import{syncScrollWithProject as l}from"../utils/syncScrollWithEditor.js";import{usePostMessage as a}from"../../providers/post-message/use-post-message.js";import{isInIframe as u}from"../../utils.js";const h=()=>{const{sendMessage:t}=a(),o=w(0),n=c(()=>{const e=window.document.querySelector(":hover"),r=Date.now()-o.current<30;if(!e||!r)return;const{percentScrolled:s,sequenceNumber:i}=l(window.scrollY);t({type:"editor-scroll-updated",percentScrolled:s,sequenceNumber:i}),t({type:"scroll-updated",position:{absolute:window.scrollY,relative:Math.round(window.scrollY/(window.document.body.clientHeight-window.innerHeight)*100)}})},[t]);d(()=>{if(!u())return;const e=()=>{o.current=Date.now()};return window.addEventListener("wheel",e),window.addEventListener("touchstart",e),window.addEventListener("keydown",e),window.addEventListener("scroll",n,{passive:!0}),()=>{window.removeEventListener("wheel",e),window.removeEventListener("touchstart",e),window.removeEventListener("keydown",e),window.removeEventListener("scroll",n)}},[n])};export{h as useScrollTracker};
