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-BqKQizRf.js";import{Y as u,Z as B,J as k}from"./index-DGg3AL9l.js";import{l as L}from"./vendor-hooks-DpqqC65c.js";import{H as P}from"./vendor-ux-KtPBvlG0.js";import{a as h,b as A,c as N}from"./spaces-DKfk45f5.js";function T(){const[r,c]=e.useState(null),{blocklet:n}=u();return e.useEffect(()=>{c(h(n))},[n]),{storageEndpoint:r}}const l=e.createContext({}),{Provider:_,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}=T(),{api:d}=B();L(async()=>{try{if(n(!0),x(s)){a(null);return}const t=await A(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 N(o.endpoint,o.name),S=G(o,{name:g});a(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 Y(){return e.useContext(l)}export{v as C,Y as u};
