import{useLocation}from"react-router-dom";import{useEffect,useLayoutEffect}from"react";import{addTrailingSlash,addLeadingSlash,IS_BROWSER,useActiveSectionId}from"@redocly/theme";import{compose,encodeBackSlashes,normalizePath}from"../../utils";let initialPageLoad=!0;export function useAutoScroll(e){const o=useLocation(),a=addTrailingSlash(e)||"/";useLayoutEffect((()=>{if(o.pathname===a&&""===o.hash)window.scrollTo(0,0);else{const e=o.hash.replace("#","").toLowerCase()||normalizePath(o.pathname).replace(a,""),t=document.getElementById(decodeURIComponent(e));t&&t.scrollIntoView()}}),[o,a]);const t=useActiveSectionId(o,!1,!1);useEffect((()=>{if(t&&initialPageLoad)initialPageLoad=!1;else if(IS_BROWSER&&!window.location.pathname.includes(t)&&!window.location.hash.includes(t)){const e=compose(addLeadingSlash,encodeBackSlashes)(t);window.history.replaceState({},"",e)}}),[t])}
//# sourceMappingURL=useAutoScroll.js.map