import{aT as E,j as e,al as m,X as V}from"./vendor-mui-core-CDEc-eG6.js";import{au as q,v as ne,G as H,cA as oe,n as ae,j as ie,W as ce,m as le,o as ue,cB as de,K as me}from"./index-DGZk149v.js";import{b as he,a as j,m as O}from"./vendor-hooks-BrgYeCrj.js";import{r as d}from"./vendor-react-DX7tU9aY.js";import{ab as pe,a2 as fe,B as ge,F as we,y as X}from"./vendor-utils-_C9URqjS.js";import{u as xe}from"./launch-result-message-Br6OdJMs.js";import{W as be,a1 as ke,O as K}from"./vendor-ux-bFYdDLNG.js";import{g as Ue}from"./get-safe-url-B1bR9jl-.js";import{S as Q}from"./button-DJOGWCMI.js";import{S as ye}from"./layout-D7JBXyAe.js";import{p as Re,P as Se,S as ve}from"./starting-progress-CRHf-USw.js";import"./vendor-arcblock-DY2miJjh.js";import"./content-layout-D49-hpq6.js";const Le=E(xe)`
  && .result-footer {
    height: 120px;
    > div {
      flex-wrap: wrap;
    }
  }
`,je="https://launcher.arcblock.io",Ee=t=>{if(!t)return je;const{protocol:c,host:s}=pe(fe(t));return`${c}//${s}`},Be=t=>t?{"__did-connect__":ge.from(JSON.stringify({switchBehavior:"auto",forceConnected:t,showClose:!1}),"utf8").toString("base64")}:{},$e=(t=0)=>new Promise(c=>setTimeout(c,t));async function Pe(t,c=5){if(!t)throw new Error("domain is required");if(typeof c!="number")throw new Error("timeout must be a number");if(c<=1)throw new Error("time must be greater than 1s");async function s(n){try{const l=`${n}://${t}`,a=await q.evaluateURL(l,{preferAccessible:!0});return a.accessible?a.url:!1}catch{return!1}}async function R(){let n=await s("https");if(n)return n;let l=c;for(;l-- >0;)if(await $e(1e3),n=await s("https"),n)return n;return n||null}const h=await R();return h?{accessible:!0,url:h}:{accessible:!1}}function Ce({onPrevious:t=()=>{},launcherSession:c={}}){const{t:s}=be(),[R,h]=d.useState(!1),[n,l]=d.useState("starting"),[a,B]=d.useState(),[g,$]=d.useState(null),{blocklet:o}=ne(),[P,C]=d.useState(!1),[p,G]=d.useState(""),S=H("fromLauncher"),J=H("launcherUrl"),Y=Ee(J),Z=Be(c?.userDid),w=!["starting","running"].includes(n),T=n==="starting",_={stopped:s("blocklet.start"),starting:s("blocklet.starting"),running:s("blocklet.open")},I=he(async()=>{const r=o?.controller?.launcherSessionId||o?.meta?.did,i=r?`/apps/${r}/overview`:"apps",u=await oe(Y);return we(X(u,"/u/",i),Z)});d.useEffect(()=>{I().then(r=>{$(r)}).catch(()=>{$(null)})},[I]);const ee=async()=>{try{const i=new URL(window.location.href).searchParams.get("redirect");if(!i)return null;const u=await Pe(i,6);return u.accessible?u.url:null}catch(r){return console.error("checkRedirectUrl error",r),null}},A=async()=>{if(S&&g){window.location.href=g;return}C(!0);const r=await ee(),u=new URL(window.location.href).searchParams.get("redirect"),M=u&&r;if(!p||!ie(p)){K.error(s("blocklet.gotoVisitError"));return}let U=p,L=[];if(M)try{const y=new URL(r);U=y.href,L=[y.hostname]}catch(y){console.error("Invalid redirect URL:",u,y)}const W=O.get("login_token"),z=O.get("vid"),f=new URL(U);f.host!==window.location.host&&W&&(f.pathname=X(ce,"redirect-with-login"),f.searchParams.set("setupToken",W),z&&f.searchParams.set("visitorId",z),U=f.href),window.location.href=Ue(U,L.length?{allowDomains:L}:{}),C(!1)},v=async()=>{h(!0),l("starting");try{await le.post("/blocklet/start?fromSetup=1");const{blocklet:{status:r}={}}=await ue.getBlocklet({input:{did:o.meta.did}});h(!1),l(r),B(r==="running"?null:s("setup.complete.notRunning"))}catch(r){const i=r?.response?.data||r.message;K.error(i),h(!1),l("stopped"),B(i)}};d.useEffect(()=>{l(o.status),["starting","running"].includes(o.status)||v()},[]),d.useEffect(()=>{p||(async()=>{const i=await q.evaluateURLs(o.site.domainAliases.map(u=>de(u.value,me)),{preferAccessible:!0});i.length>0&&G(i[0].url)})()},[o,p]);const x=j(()=>o?.children?.length,[o]),b=j(()=>o?.children?.filter(r=>ae.isBlockletRunning(r))?.length||0,[o]),k=j(()=>x?b?Math.round(Math.max(b/x,.15)*100):5:0,[b,x]),D=localStorage.getItem("blocklet-server-url"),F=T||!p;S&&!F&&(w?v():A());const te=ke(Re,w?{ml:3}:{},T?{width:`${k}%`,"&::after":{width:`${k}%`},"&::before":{width:`${k}%`,animation:"shimmer 1s infinite cubic-bezier(0.4, 0.0, 0.2, 1)","@keyframes shimmer":{"0%":{width:0},"100%":{width:`${k}%`}}}}:{}),re={starting:s("setup.complete.readyToStart"),running:s("setup.complete.title")},se=()=>P?s("setup.complete.opening"):n==="starting"&&!a?`(${b}/${x}) ${s("blocklet.starting")}`:_[n]||_.stopped,N=e.jsx(Se,{blocklets:o.children??[],status:n,debounce:!1,children:e.jsx(Q,{sx:te,onClick:()=>w?v():A(),loading:F||P,children:e.jsx("span",{style:{position:"relative",zIndex:2},children:se()})})});return e.jsx(Te,{children:e.jsx(Le,{variant:a?"error":"success",title:e.jsxs(m,{sx:{display:"flex",flexDirection:"column",alignItems:"center",gap:1},children:[e.jsx(m,{sx:{fontSize:24,fontWeight:600},children:a?s("setup.complete.failedTitle"):re[n]||""}),n==="starting"&&!a?e.jsx(ve,{blocklets:o.children??[]}):null]}),footer:e.jsxs(_e,{children:[w&&e.jsx(Q,{variant:"outlined",disabled:R,onClick:()=>t(),children:s("setup.previous")}),S?e.jsxs(e.Fragment,{children:[e.jsx(m,{sx:{display:"flex",flexDirection:"column",alignItems:"center",gap:1},children:N}),a&&g?e.jsx(m,{sx:{width:"100%",mt:2},children:e.jsx("a",{href:g,target:"_blank",rel:"noopener noreferrer",className:"back-to-server-link",children:s("launchBlocklet.nav.viewMyApps")})}):null]}):e.jsxs(e.Fragment,{children:[e.jsx(m,{sx:{display:"flex",flexDirection:"column",alignItems:"center",gap:1},children:N}),(n==="running"||a)&&!!D&&e.jsx(m,{sx:{width:"100%",mt:2},children:e.jsx("a",{href:D,target:"_blank",rel:"noopener noreferrer",className:"back-to-server-link",children:s("setup.complete.backToServer")})}),a&&e.jsx(m,{className:"error-message",sx:{fontSize:14,color:"#888",mt:1},children:a})]})]})})})}const Te=E(ye)`
  justify-content: center;
  .icon-success {
    width: 64px;
    height: 64px;
    color: ${t=>t.theme.palette.success.main};
  }
  .back-to-server-link {
    color: ${t=>t.theme.palette.primary.main};
  }
  .error-message {
    color: ${t=>t.theme.palette.error.main};
    height: 15em;
    overflow: hidden;
    overflow-y: scroll;
  }
`;Ce.propTypes={onPrevious:V.func,launcherSession:V.object};const _e=E(m)`
  width: 100%;
  margin-top: auto;
  padding-top: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  .bottom-button {
    min-width: 200px;
  }
  ${t=>t.theme.breakpoints.down("md")} {
    .bottom-button {
      width: 100%;
    }
  }
`;export{Ce as default};
