import{W as g}from"./index-CgSpL7jz.js";import{a3 as E}from"./vendor-ux-PO0jFgJf.js";import{b as y}from"./vendor-hooks-Ca2cSNuD.js";import{r as m}from"./vendor-react-DX7tU9aY.js";import{g as h}from"./get-safe-url-uxJSMQTF.js";import{e as T}from"./vendor-utils-_C9URqjS.js";function $({messageType:n,onOpen:o,onClose:r}){const e=m.useRef(null),t=m.useRef(null),w=async s=>{setTimeout(()=>{try{r?.()}catch{}},10*1e3),e.current&&(clearInterval(t.current),e.current.close());const a=Math.random().toString(36).slice(2);e.current=window.open(`${g}/open-window?key=${a}`,"_blank"),o?.(e.current),t.current=setInterval(()=>{(!e.current||e.current.closed)&&(e.current=null,clearInterval(t.current),setTimeout(()=>{r?.()},1100))},200),await Promise.resolve(s(e.current,c=>{if(!e.current){localStorage.setItem(`open-window-url-${a}`,JSON.stringify({url:c,time:Date.now()}));return}e.current.location.href=h(c,{allowDomains:null})}))},i=y(s=>{const{type:a,error:c}=s.data;e.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:e=4e3,timerKey:t=""}){u[t]&&clearTimeout(u[t]),await new Promise(s=>setTimeout(s,e));const w=Date.now(),i=async(s,a)=>{try{const c=await n();s(c)}catch(c){Date.now()-w>r?a(c):t?u[t]=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:e}){return l(async()=>{const t=await n.getProject({input:{did:o,projectId:r,messageId:e}});if(!t?.project?.blockletDid)throw new Error("Connected store not found");return t.project.blockletDid},{timerKey:"connect-studio"})}function O(n,{did:o,projectId:r,storeId:e}){return l(async()=>{const t=await n.getProject({input:{did:o,projectId:r,messageId:""}});if(!t?.project?.connectedStores?.length)throw new Error("Connected store not found");if(!t.project.connectedStores.find(i=>i.storeId===e))throw new Error("Connected store not found")},{timerKey:`connect-store-${e}`})}function W(n,{did:o,projectId:r,endpointId:e}){return l(async()=>{const t=await n.getProject({input:{did:o,projectId:r,messageId:""}});if(!t?.project?.connectedEndpoints?.length)throw new Error("Connected endpoint not found");if(!t.project.connectedEndpoints.find(i=>i.endpointId===e))throw new Error("Connected endpoint not found")},{timerKey:`connect-store-${e}`})}function M(n,{did:o,refresh:r,t:e,providerName:t,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(e("setting.aigne.connectTimeOut",{provider:t}));if(d&&d===f)throw new Error(e("setting.aigne.connectTimeOut",{provider:t}));await r(),clearTimeout(u[`connect-aigne-${o}`])}else{if(!d||d!==f)throw new Error(e("setting.aigne.connectTimeOut",{provider:t}));await r(),clearTimeout(u[`connect-aigne-${o}`])}},{timerKey:i,maxDuration:180*1e3})}export{j as a,v as b,b as c,O as d,W as e,$ as u,M as w};
