import{r as e}from"./vendor-react-CUXIiAnY.js";import{j as C,bB as w}from"./vendor-mui-core-BPQ6AEW_.js";import{a as x,Q as B}from"./vendor-utils-BgSaqjGx.js";import{ac as u,_ as G,Y as k}from"./index-DY2wgD65.js";import{j as L}from"./vendor-hooks-PN9SWiQm.js";import{H as P}from"./vendor-ux-r9bApERu.js";import{g as _,a as h,b as A}from"./spaces-Cgv_XshJ.js";function N(){const[r,c]=e.useState(null),{blocklet:a}=u();return e.useEffect(()=>{c(_(a))},[a]),{storageEndpoint:r}}const l=e.createContext({}),{Provider:T,Consumer:Y}=l;function b({children:r}){const[c,a]=e.useState(!1),[o,n]=e.useState(null),{blocklet:f,actions:{refreshBlocklet:i}}=u(),{storageEndpoint:s}=N(),{api:d}=G();L(async()=>{try{if(a(!0),x(s)){n(null);return}const t=await h(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 A(o.endpoint,o.name),S=B(o,{name:g});n(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})}b.propTypes={children:w.node.isRequired};function q(){return e.useContext(l)}export{b as C,q as u};
