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