import{withPathPrefix as m}from"@redocly/theme";import{isLocalLink as u}from"../../../shared/utils";import{withLoadProgress as p}from"./withLoadProgress";let l;async function L({navigate:i,to:o,origin:r="browser",options:a}){o===""&&(o="/"),l=o;const{pathname:n,hash:w,search:c}=new URL(o,window.location.origin+window.location.pathname),t=decodeURIComponent(w),h=decodeURIComponent(window.location.hash),f=window.__LOADER,e=await p(f.tryLoad(n));if(e?.redirectTo){if(!u(e.redirectTo)){window.location.href=e.redirectTo;return}return L({navigate:i,to:e.redirectTo,origin:r,options:a})}if(o.startsWith(m("/_auth/"))){window.location.href=o;return}if(e&&l===o){if((n!==window.location.pathname||c!==window.location.search||t!==h)&&i({pathname:n,search:c,hash:t},{...a,state:{origin:r},unstable_flushSync:!0}),e.props?.disableAutoScroll)return;setTimeout(()=>{if(t){const s=t.slice(1),d=document.getElementById(s)||document.getElementsByName(s)[0];d&&d.scrollIntoView()}else window.scrollTo(0,0)},0)}}export{L as loadAndNavigate};
