import{r as e}from"./vendor-react-CTtPmzVP.js";import{j as C,cq as w}from"./vendor-mui-core-DnR6WYuU.js";import{a as x,_ as G}from"./vendor-utils-CxWN1dT3.js";import{Y as u,Z as B,J as k}from"./index-BZxsCCR_.js";import{l as L}from"./vendor-hooks-qxhUUbW5.js";import{H as P}from"./vendor-ux-DTEmWsko.js";import{a as _,b as h,c as A}from"./spaces-DBCz6tUn.js";function N(){const[r,c]=e.useState(null),{blocklet:n}=u();return e.useEffect(()=>{c(_(n))},[n]),{storageEndpoint:r}}const l=e.createContext({}),{Provider:T,Consumer:q}=l;function v({children:r}){const[c,n]=e.useState(!1),[o,a]=e.useState(null),{blocklet:f,actions:{refreshBlocklet:i}}=u(),{storageEndpoint:s}=N(),{api:d}=B();L(async()=>{try{if(n(!0),x(s)){a(null);return}const t=await h(s);a(t)}catch(t){console.error(t),P.error(t.message)}finally{n(!1)}},[s]);const m=()=>{a(null),i()},E=async t=>{a(t),await p(t.endpoint),i()},p=async t=>{await d.configBlocklet({input:{did:[f.meta.did],configs:[{key:k.BLOCKLET_CONFIGURABLE_KEY.BLOCKLET_APP_SPACE_ENDPOINT,value:t}]}}),i()};e.useEffect(()=>{async function t(){if(!o)return;const g=await A(o.endpoint,o.name),S=G(o,{name:g});a(S)}t()},[]);const y=!!(s&&o);return C.jsx(T,{value:{loading:c,spaceGateway:o,deleteSpaceGateway:m,updateSpaceGateway:E,storageEndpoint:s,settingStorageEndpoint:p,hasStorageEndpoint:y},children:r})}v.propTypes={children:w.node.isRequired};function Y(){return e.useContext(l)}export{v as C,Y as u};
