import s,{createContext as E,useEffect as U}from"react";import{useLocation as L,useNavigate as x}from"react-router-dom";import{useColorSwitcher as O}from"@redocly/theme/core/hooks";import{withPathPrefix as T}from"@redocly/theme/core/utils";import{ServerRoutes as z}from"../../../constants/api";import{removeTrailingSlash as D}from"../../../utils/url/remove-trailing-slash";import{normalizeRouteSlug as P}from"../../../utils/path/normalize-route-slug";import{loadAndNavigate as I}from"../../app/utils/loadAndNavigate";import{resolveRouteBySlug as N}from"../../app/utils/resolveRouteBySlug";import{syncScrollWithEditor as A}from"../../app/utils/syncScrollWithEditor";import{PostMessageConnection as H}from"./PostMessageConnection";import{isHostnameOnDomain as i}from"../../app/utils/isHostnameOnDomain";import{usePageData as _}from"../../app/hooks/usePageData";const B=E({sendMessage:()=>{}});function ae({children:l,enabled:t}){const R=x(),n=L(),c=s.useRef(null),{switchColorMode:h}=O(),p=_()?.templateId,u=s.useCallback(e=>{c.current?.send(e)},[]),f=s.useCallback(async e=>{console.info("[PM] connection initialized."),c.current=new H(e.source,e.origin);const a=await N(n.pathname+n.hash,p).catch(o=>{console.error("Initial route resolve failed with error: ",o.message)});u({type:"initialized",initialRoute:a})},[u,n.pathname,n.hash,p]),g=s.useCallback(async e=>{const{path:a="/",line:o=1,url:m}=e;let r=m;if(!m)try{const d=await fetch(T(z.RESOLVE_ROUTE_BY_PATH),{method:"POST",body:JSON.stringify(a+(o?`:${o}`:""))}),{route:S}=await d.json();r=S?.slug??S?.url??void 0}catch(d){console.error(d)}if(!r)return;r=r==="/"?"/":D(r);const[v,C]=r.split("#"),y=P(v)===P(n.pathname),M=(C??"")===n.hash.substring(1);(m?y&&M:y)||(console.info("[PM] Update route to: ",r),await I({navigate:R,to:r,origin:"pm"}))},[n]),w=s.useCallback(async e=>{const{action:a}=e;switch(a){case"back":window.history.back();break;case"forward":window.history.forward();break;case"reload":window.location.reload();break}},[]),b=s.useCallback(e=>{window.scrollTo({top:e.position.absolute,behavior:"instant"})},[]),k=s.useCallback(e=>{h(e.colorSchema)},[h]);return U(()=>{if(!t)return;const e=async a=>{if(!j(a.origin))return;const o=a.data;switch(o.type){case"initialize":f(a);break;case"route-update":if(!c.current)return;g(o);break;case"location-update":if(!c.current)return;w(o);break;case"scroll-update":if(!c.current)return;b(o);break;case"editor-scroll-update":if(!c.current)return;A(o.percentScrolled,o.sequenceNumber);break;case"color-schema-update":if(!c.current)return;k(o);break}};return window.addEventListener("message",e),()=>{window.removeEventListener("message",e)}},[t,f,g,w,b,k]),s.createElement(B.Provider,{value:{sendMessage:u}},l)}function j(l){const t=new URL(l);return i(t.hostname,"localhost")||i(t.hostname,"127.0.0.1")||i(t.hostname,"blueharvest.cloud")||i(t.hostname,"bhstage.cloud")||i(t.hostname,"cloud.redocly.com")||i(t.hostname,"cloud.eu.redocly.com")||i(t.hostname,"cba.au.redocly.com")}export{B as PostMessageContext,ae as PostMessageProvider};
