import{c as g}from"./index-DY2wgD65.js";import{H as E}from"./vendor-ux-r9bApERu.js";import{b as y}from"./vendor-hooks-PN9SWiQm.js";import{r as m}from"./vendor-react-CUXIiAnY.js";import{g as h}from"./get-safe-url-DqysS0i0.js";import{e as T}from"./vendor-utils-BgSaqjGx.js";function $({messageType:n,onOpen:o,onClose:r}){const t=m.useRef(null),e=m.useRef(null),w=async s=>{setTimeout(()=>{try{r?.()}catch{}},10*1e3),t.current&&(clearInterval(e.current),t.current.close());const a=Math.random().toString(36).slice(2);t.current=window.open(`${g.WELLKNOWN_SERVICE_PATH_PREFIX}/open-window?key=${a}`,"_blank"),o?.(t.current),e.current=setInterval(()=>{(!t.current||t.current.closed)&&(t.current=null,clearInterval(e.current),setTimeout(()=>{r?.()},1100))},200),await Promise.resolve(s(t.current,c=>{if(!t.current){localStorage.setItem(`open-window-url-${a}`,JSON.stringify({url:c,time:Date.now()}));return}t.current.location.href=h(c,{allowDomains:null})}))},i=y(s=>{const{type:a,error:c}=s.data;t.current&&a===n&&c&&(E.error(c),r?.())});return m.useEffect(()=>(window.addEventListener("message",i),()=>{window.removeEventListener("message",i)}),[i]),w}const u={};function b(n){u[n]&&(clearTimeout(u[n]),delete u[n])}async function l(n,{interval:o=1e3,maxDuration:r=120*1e3,intervalFirst:t=4e3,timerKey:e=""}){u[e]&&clearTimeout(u[e]),await new Promise(s=>setTimeout(s,t));const w=Date.now(),i=async(s,a)=>{try{const c=await n();s(c)}catch(c){Date.now()-w>r?a(c):e?u[e]=setTimeout(()=>i(s,a),o):setTimeout(()=>i(s,a),o)}};return new Promise(i)}function j(){return Math.random().toString(36).slice(2)+Math.random().toString(36).slice(2)+`${Date.now()}`.slice(-6)}function v(n,{did:o,projectId:r,messageId:t}){return l(async()=>{const e=await n.getProject({input:{did:o,projectId:r,messageId:t}});if(!e?.project?.blockletDid)throw new Error("Connected store not found");return e.project.blockletDid},{timerKey:"connect-studio"})}function O(n,{did:o,projectId:r,storeId:t}){return l(async()=>{const e=await n.getProject({input:{did:o,projectId:r,messageId:""}});if(!e?.project?.connectedStores?.length)throw new Error("Connected store not found");if(!e.project.connectedStores.find(i=>i.storeId===t))throw new Error("Connected store not found")},{timerKey:`connect-store-${t}`})}function M(n,{did:o,projectId:r,endpointId:t}){return l(async()=>{const e=await n.getProject({input:{did:o,projectId:r,messageId:""}});if(!e?.project?.connectedEndpoints?.length)throw new Error("Connected endpoint not found");if(!e.project.connectedEndpoints.find(i=>i.endpointId===t))throw new Error("Connected endpoint not found")},{timerKey:`connect-store-${t}`})}function R(n,{did:o,refresh:r,t,providerName:e,values:w,timerKey:i}){return l(async()=>{const{blocklet:s}=await n.getBlocklet({input:{did:o}}),a=T(s,"settings.aigne",{}),{url:c,key:f,provider:p}=a||{},{key:d}=w;if(p==="aigneHub"){if(!c||!f)throw new Error(t("setting.aigne.connectTimeOut",{provider:e}));if(d&&d===f)throw new Error(t("setting.aigne.connectTimeOut",{provider:e}));await r(),clearTimeout(u[`connect-aigne-${o}`])}else{if(!d||d!==f)throw new Error(t("setting.aigne.connectTimeOut",{provider:e}));await r(),clearTimeout(u[`connect-aigne-${o}`])}},{timerKey:i,maxDuration:3*60*1e3})}export{O as a,M as b,j as c,b as d,R as e,$ as u,v as w};
