import{useLocation as u,useNavigate as l,useNavigationType as d}from"react-router-dom";import{useEffect as v,useState as P}from"react";import{useThemeConfig as w}from"@redocly/theme";import{onRouteChange as y}from"../../runtime/generated/browser-plugins.js";import{resolveRouteBySlug as E}from"../utils/resolveRouteBySlug.js";import{usePageData as M}from"./usePageData.js";import{usePostMessage as T}from"../../providers/post-message/use-post-message.js";import{isInIframe as C}from"../../utils.js";import{removeTrailingSlash as b}from"../../../shared/utils.js";import{waitForContentScripts as I}from"../../ScriptLoader.js";import{otelTelemetry as N}from"../telemetry/otel/telemetry.js";const W=()=>{const e=u(),n=l(),a=d(),r=M(),[o,i]=P(),m=w(),{sendMessage:h}=T();v(()=>{I().then(()=>{if(e.pathname!==o?.pathname||e.search!==o.search){y({location:e,prevLocation:o},m);const t=`${window.origin}${e.pathname+e.search+e.hash}`,s=o?`${window.origin}${o.pathname+o.search+o.hash}`:void 0;N.send({type:"page.viewed",payload:{uri:t,referrer:s||document.referrer||void 0}})}});const p=!!o;i(e);const{pathname:c,hash:g,state:f}=e;if(e?.pathname.endsWith("/")&&e?.pathname!=="/"){const t=b(e.pathname);n({pathname:t,search:e.search,hash:e.hash},{replace:!0})}(process.env.NODE_ENV==="development"||C())&&p&&E(c+g,r?.templateId).then(t=>{const s=a==="POP"?"browser":f?.origin;s&&h({type:"route-updated",origin:s,...t})}).catch(t=>console.log("Resolve route by slug failed with error: ",t.message))},[e,a])};export{W as useRouteChangeTracker};
