import{e as W,aW as J,aX as X,V as Y,f as K,r as i,o as Q,G as L,aq as T,j as _,aQ as Z,aY as F,aZ as ee,a1 as te,a2 as re,h as ne,F as oe,n as ae,S as se,a_ as ie,aR as ce}from"./index.RuhrnD1v.js";import{a$ as Me}from"./index.RuhrnD1v.js";import{w as le}from"./withCalculatedWidth.C2I3eFeZ.js";import{C as $,a as P}from"./urls.BwSlolu9.js";import{D as ue,a as me}from"./IFrameUtil.DefezniK.js";var C=(function(e){return e.COMPONENT_READY="streamlit:componentReady",e.SET_COMPONENT_VALUE="streamlit:setComponentValue",e.SET_FRAME_HEIGHT="streamlit:setFrameHeight",e})({}),de=(function(e){return e.RENDER="streamlit:render",e})({});const fe=1,p=Y.getLogger("componentUtils");function ge(e){return(r,t)=>{if(!e.current)return;const{isReady:o,element:n,widgetMgr:a,setComponentError:R,componentReadyCallback:y,frameHeightCallback:w,fragmentId:c}=e.current,s=o();switch(r){case C.COMPONENT_READY:{const{apiVersion:m}=t;m!==fe?R(new Error(`Unrecognized component API version: '${m}'`)):y();break}case C.SET_COMPONENT_VALUE:s?he(D(t,"value"),t.dataType,{fromUi:!0},n,a,c):p.warn(`Got ${r} before ${C.COMPONENT_READY}!`);break;case C.SET_FRAME_HEIGHT:s?w(D(t,"height")):p.warn(`Got ${r} before ${C.COMPONENT_READY}!`);break;default:p.warn(`Unrecognized ComponentBackMsgType: ${r}`)}}}function pe(e,r){const t=JSON.parse(e),o=[];for(const n of r){const{key:a}=n;switch(n.value?.toLowerCase()){case"arrowdataframe":o.push({key:a,value:X.toObject(n.arrowDataframe)});break;case"bytes":t[a]=n.bytes;break;default:throw new Error(`Unrecognized SpecialArg type: ${n.value}`)}}return[t,o]}function x(e,r,t,o,n){if(!n){p.warn("Can't send ForwardMsg; missing our iframe!");return}if(W(n.contentWindow)){p.warn("Can't send ForwardMsg; iframe has no contentWindow!");return}n.contentWindow.postMessage({type:de.RENDER,args:e,dfs:r,disabled:t,theme:{...J(o),font:o.genericFonts.bodyFont}},"*")}function he(e,r,t,o,n,a){if(e===void 0){p.warn("handleSetComponentValue: missing 'value' prop");return}switch(r){case"dataframe":n.setArrowValue(o,e,t,a);break;case"bytes":n.setBytesValue(o,e,t,a);break;default:n.setJsonValue(o,e,t,a)}}function D(e,r,t=void 0){return Object.hasOwn(e,r)?e[r]:t}const Ee=K("iframe",{target:"edxyb2x0"})(({theme:e,componentReady:r})=>({colorScheme:"normal",border:"none",padding:e.spacing.none,margin:e.spacing.none,display:r?"initial":"none"})),A=Y.getLogger("ComponentInstance"),V=6e4;function Ce(e,r,t){let o;ae(t)&&t!==""?o=t:o=r.getComponentURL(e,"index.html");const n=se.CUSTOM_COMPONENT_CLIENT_ID,a=new URL(window.location.href);return o=ie.stringifyUrl({url:o,query:{streamlitUrl:a.origin+a.pathname,...n&&{__streamlit_parent_client_id:n}}}),o}function H(e,r){let t;return r&&r!==""?t=`Your app is having trouble loading the **${e}** component.
The app is attempting to load the component from **${r}**,
and hasn't received its \`Streamlit.setComponentReady()\` message.

If this is a development build, have you started the dev server?

For more troubleshooting help, please see the [Streamlit Component docs](${$}) or visit our [forums](${P}).`:t=`Your app is having trouble loading the **${e}** component.

If this is an installed component that works locally, the app may be having trouble accessing the component frontend assets due to network latency or proxy settings in your app deployment.

For more troubleshooting help, please see the [Streamlit Component docs](${$}) or visit our [forums](${P}).`,t}function Re(e,r,t,o){if(!o)try{return pe(e,r)}catch(n){const a=ce(n);t(a)}return[{},[]]}function ye(e,r){return e===r||e.length===r.length&&e.every((t,o)=>{const n=r[o];return t.key===n.key&&t.value===n.value})}function we(e){const r=Q(),[t,o]=i.useState(),{disabled:n,element:a,widgetMgr:R,width:y,fragmentId:w,componentRegistry:c}=e,{componentName:s,jsonArgs:m,specialArgs:G,url:S}=a,[O,b]=Re(m,G,o,t),h=i.useMemo(()=>Ce(s,c,S),[s,c,S]),l=i.useRef({args:{},dataframeArgs:[]}),M=ye(l.current.dataframeArgs,b);l.current.args=O,l.current.dataframeArgs=b;const[E,I]=i.useState(),[d,j]=i.useState(()=>isNaN(O.height)?void 0:O.height),u=i.useRef(!1),f=i.useRef(null),v=i.useRef(),{clear:k}=L(()=>A.warn(H(s,S)),V/4),{clear:U}=L(()=>{T.flushSync(()=>{I(!0)})},V);if(i.useEffect(()=>{c.checkSourceUrlResponse(h,s)},[h,s,c]),i.useEffect(()=>{E&&!u.current&&(A.error(`Client Error: Custom Component ${s} timeout error`),c.sendTimeoutError(h,s))},[E,h,s,c]),i.useEffect(()=>{u.current&&x(l.current.args,l.current.dataframeArgs,n,r,f.current??void 0)},[n,d,M,m,r,y]),i.useEffect(()=>{const g=N=>{if(N===void 0){A.warn("handleSetFrameHeight: missing 'height' prop");return}if(N!==d){if(W(f.current)){A.warn("handleSetFrameHeight: missing our iframeRef!");return}f.current.height=N.toString(),T.flushSync(()=>{j(N)})}},z=()=>{x(l.current.args,l.current.dataframeArgs,n,r,f.current??void 0),k(),U(),u.current=!0,T.flushSync(()=>{I(!1)})};v.current={isReady:()=>u.current,element:a,widgetMgr:R,setComponentError:o,componentReadyCallback:z,frameHeightCallback:g,fragmentId:w}},[s,n,a,d,M,E,m,r,R,U,k,w]),i.useEffect(()=>{const g=f.current?.contentWindow??void 0;if(g)return c.registerListener(g,ge(v)),()=>{g&&c.deregisterListener(g)}},[c,s]),t)return _(Z,{name:t.name,message:t.message});const B=!u.current&&!E&&d!==0&&_(ee,{element:F.create({height:d,style:F.SkeletonStyle.ELEMENT})}),q=!u.current&&E?_(re,{body:H(s,S),kind:te.WARNING}):null;return ne(oe,{children:[B,q,_(Ee,{className:"stCustomComponentV1","data-testid":"stCustomComponentV1",allow:me,ref:f,src:h,width:y,height:d??0,scrolling:"no",sandbox:ue,title:s,componentReady:u.current,tabIndex:a.tabIndex??void 0})]})}const Oe=le(i.memo(we));export{Oe as ComponentInstance,Me as ComponentRegistry};
