import{useLocation as f,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{normalizeRouteSlug as E}from"../../../utils/path/normalize-route-slug";import{getBaseDataAttributes as M}from"@redocly/theme/core/utils";import{onRouteChange as y}from"../../runtime/generated/browser-plugins.js";import{resolveRouteBySlug as b}from"../utils/resolveRouteBySlug.js";import{usePageData as C}from"./usePageData.js";import{usePostMessage as R}from"../../providers/post-message/use-post-message.js";import{isInIframe as D}from"../../utils/utils.js";import{waitForContentScripts as I}from"../../ScriptLoader.js";import{telemetry as N}from"../telemetry/index.js";const _=()=>{const e=f(),r=d(),a=l(),n=C(),[t,i]=P(),m=w(),{sendMessage:h}=R();v(()=>{if(e?.pathname.endsWith("/")&&e?.pathname!=="/"){r({pathname:E(e.pathname),search:e.search,hash:e.hash},{replace:!0,state:e.state});return}I().then(()=>{if(e.pathname!==t?.pathname||e.search!==t.search){y({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([{...M(o,"page",o),referrer:s||document.referrer||void 0}])}});const p=!!t;i(e);const{pathname:c,hash:g,state:u}=e;(process.env.NODE_ENV==="development"||D())&&p&&b(c+g,n?.templateId).then(o=>{const s=a==="POP"?"browser":u?.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{_ as useRouteChangeTracker};
