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