import{s as v}from"./chunk-LvLJmgfZ.js";import{i as j}from"./useEvent-WUlsM6MC.js";import{t as y}from"./react-Bj1aDYRI.js";import{Qr as c,Zr as w,_n as S,at as R,m as _,ot as H,pn as M}from"./cells-CCtxWKxf.js";import{t as E}from"./compiler-runtime-B3qBwwSJ.js";import{m as N}from"./useEventListener-CjXriKgk.js";import{t as T}from"./jsx-runtime-CP2fAiZX.js";import{t as C}from"./usePress-DsLftN-x.js";import{n as F}from"./useDebounce-BUC67G8E.js";import{t as I}from"./copy-icon-6RnnFDPt.js";var k=E(),m=v(y(),1),o=v(T(),1);const q=r=>{let t=(0,k.c)(16),e,n,i;t[0]===r?(e=t[1],n=t[2],i=t[3]):({href:n,children:e,...i}=r,t[0]=r,t[1]=e,t[2]=n,t[3]=i);let a=(0,m.useRef)(null),l;t[4]===n?l=t[5]:(l=()=>{let u=new URL(globalThis.location.href);u.hash=n,globalThis.history.pushState({},"",u.toString()),globalThis.dispatchEvent(new HashChangeEvent("hashchange"));let x=n.slice(1),g=document.getElementById(x);g&&g.scrollIntoView({behavior:"smooth",block:"start"})},t[4]=n,t[5]=l);let s=l,f;t[6]===s?f=t[7]:(f={onPress:()=>{s()}},t[6]=s,t[7]=f);let{pressProps:h}=C(f),d;t[8]===s?d=t[9]:(d=u=>{u.preventDefault(),s()},t[8]=s,t[9]=d);let b=d,p;return t[10]!==e||t[11]!==b||t[12]!==n||t[13]!==h||t[14]!==i?(p=(0,o.jsx)("a",{ref:a,href:n,...h,onClick:b,...i,children:e}),t[10]=e,t[11]=b,t[12]=n,t[13]=h,t[14]=i,t[15]=p):p=t[15],p};async function z(r){let t=_().inOrderIds.at(0);if(t)try{let e=await M.request({document:r,cellId:t});if(!e||e.options.length===0)return;let n=r.split(".").pop()??r,i=e.options[0],a=e.options.find(l=>l.name===n)??i;a!=null&&a.completion_info&&j.set(S,{documentation:a.completion_info})}catch(e){N.debug(`Doc lookup failed for "${r}"`,e)}}var L=E();const V=r=>{let t=(0,L.c)(8),{qualifiedName:e,children:n}=r,i;t[0]===e?i=t[1]:(i=()=>{z(e)},t[0]=e,t[1]=i);let a=F(i,100),l;t[2]===a?l=t[3]:(l=()=>a.cancel(),t[2]=a,t[3]=l);let s;return t[4]!==n||t[5]!==a||t[6]!==l?(s=(0,o.jsx)("span",{onMouseEnter:a,onMouseLeave:l,children:n}),t[4]=n,t[5]=a,t[6]=l,t[7]=s):s=t[7],s};var W=E(),A=r=>{if(r instanceof c.Element&&!/^[A-Za-z][\w-]*$/.test(r.name))return m.createElement(m.Fragment)},D=(r,t)=>{if(t instanceof c.Element&&t.name==="body"){if((0,m.isValidElement)(r)&&"props"in r){let e=r.props.children;return(0,o.jsx)(o.Fragment,{children:e})}return}},O=(r,t)=>{if(t instanceof c.Element&&t.name==="html"){if((0,m.isValidElement)(r)&&"props"in r){let e=r.props.children;return(0,o.jsx)(o.Fragment,{children:e})}return}},P=r=>{if(r instanceof c.Element&&r.attribs&&r.name==="iframe"){let t=document.createElement("iframe");return Object.entries(r.attribs).forEach(([e,n])=>{e.startsWith('"')&&e.endsWith('"')&&(e=e.slice(1,-1)),t.setAttribute(e,n)}),(0,o.jsx)("div",{dangerouslySetInnerHTML:{__html:t.outerHTML}})}},$=r=>{if(r instanceof c.Element&&r.name==="script"){let t=r.attribs.src;if(!t)return;if(!document.querySelector(`script[src="${t}"]`)){let e=document.createElement("script");e.src=t,document.head.append(e)}return(0,o.jsx)(o.Fragment,{})}},Z=(r,t)=>{if(t instanceof c.Element&&t.name==="a"){let e=t.attribs.href;if(e!=null&&e.startsWith("#")&&!e.startsWith("#code/")){let n=null;return(0,m.isValidElement)(r)&&"props"in r&&(n=r.props.children),(0,o.jsx)(q,{href:e,...t.attribs,children:n})}}},B=(r,t,e)=>{var n,i;if(t instanceof c.Element&&t.name==="div"&&((i=(n=t.attribs)==null?void 0:n.class)!=null&&i.includes("codehilite")))return(0,o.jsx)(U,{children:r},e)},Q=(r,t)=>{var e;if(t instanceof c.Element&&((e=t.attribs)!=null&&e["data-marimo-doc"])){let n=t.attribs["data-marimo-doc"];return(0,o.jsx)(V,{qualifiedName:n,children:r})}},U=r=>{let t=(0,W.c)(3),{children:e}=r,n=(0,m.useRef)(null),i;t[0]===Symbol.for("react.memo_cache_sentinel")?(i=(0,o.jsx)("div",{className:"absolute top-2 right-2 opacity-0 group-hover:opacity-100 transition-opacity",children:(0,o.jsx)(I,{tooltip:!1,className:"p-1",value:()=>{var s;let l=(s=n.current)==null?void 0:s.firstChild;return l&&l.textContent||""}})}),t[0]=i):i=t[0];let a;return t[1]===e?a=t[2]:(a=(0,o.jsxs)("div",{className:"relative group codehilite-wrapper",ref:n,children:[e,i]}),t[1]=e,t[2]=a),a};const G=({html:r,additionalReplacements:t=[],alwaysSanitizeHtml:e=!0})=>(0,o.jsx)(J,{html:r,alwaysSanitizeHtml:e,additionalReplacements:t});var J=({html:r,additionalReplacements:t=[],alwaysSanitizeHtml:e})=>{let n=H();return K({html:(0,m.useMemo)(()=>e||n?R(r):r,[r,e,n]),additionalReplacements:t})};function K({html:r,additionalReplacements:t=[]}){let e=[A,P,$,...t],n=[B,Z,Q,D,O];return w(r,{replace:(i,a)=>{for(let l of e){let s=l(i,a);if(s)return s}return i},transform:(i,a,l)=>{for(let s of n){let f=s(i,a,l);if(f)return f}return i}})}export{G as t};
