import{r as e}from"./vendor-react-DX7tU9aY.js";import{j as C,cp as w}from"./vendor-mui-core-D7t4vMS5.js";import{a as x,$ as G}from"./vendor-utils-BzQg432x.js";import{V as u,W as B,G as k}from"./index-Ca33rdd_.js";import{p as L}from"./vendor-hooks-DAZ1H6Va.js";import{Q as P}from"./vendor-ux-Dl3wVbv_.js";import{a as h,b as A,c as N}from"./spaces-Ckljlc4d.js";function T(){const[r,c]=e.useState(null),{blocklet:a}=u();return e.useEffect(()=>{c(h(a))},[a]),{storageEndpoint:r}}const l=e.createContext({}),{Provider:_,Consumer:$}=l;function v({children:r}){const[c,a]=e.useState(!1),[o,n]=e.useState(null),{blocklet:f,actions:{refreshBlocklet:i}}=u(),{storageEndpoint:s}=T(),{api:d}=B();L(async()=>{try{if(a(!0),x(s)){n(null);return}const t=await A(s);n(t)}catch(t){console.error(t),P.error(t.message)}finally{a(!1)}},[s]);const m=()=>{n(null),i()},E=async t=>{n(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 N(o.endpoint,o.name),S=G(o,{name:g});n(S)}t()},[]);const y=!!(s&&o);return C.jsx(_,{value:{loading:c,spaceGateway:o,deleteSpaceGateway:m,updateSpaceGateway:E,storageEndpoint:s,settingStorageEndpoint:p,hasStorageEndpoint:y},children:r})}v.propTypes={children:w.node.isRequired};function q(){return e.useContext(l)}export{v as C,q as u};
