import{useLocation}from"react-router-dom";import{useEffect}from"react";import{addLeadingSlash,IS_BROWSER,useActiveSectionId,removeLeadingSlash}from"@redocly/theme";import{compose,encodeBackSlashes,tryDecodeURIComponent}from"../../utils";let initialPageLoad=!0;export function useAutoScroll(e){const o=useLocation(),t=e||"/";useEffect((()=>{if(o.pathname===t&&""===o.hash)return void window.scrollTo(0,0);const e=removeLeadingSlash(o.pathname.replace(t,"")),a=getLegacyHash(o.hash),n=a?getElementById(e+a)||document.querySelector(`[id$="${removeLeadingSlash(a)}"]`):getElementById(o.hash.replace("#","").toLowerCase())||getElementById(e);n&&n.scrollIntoView()}),[o,t]);const a=useActiveSectionId(o,!1,!1);useEffect((()=>{if(a&&initialPageLoad)initialPageLoad=!1;else if(IS_BROWSER&&!window.location.pathname.includes(a)&&!window.location.hash.includes(a)){const e=compose(addLeadingSlash,encodeBackSlashes)(a);window.history.replaceState({},"",e)}}),[a])}export function getElementById(e){return document.getElementById(tryDecodeURIComponent(e))}export function getLegacyHash(e){const o=["#tag","#operation","#paths"];for(const t of o)if(e.includes(t))return e.replace(t,"").toLowerCase();return""}
//# sourceMappingURL=useAutoScroll.js.map