import{aT as E,j as e,al as m,X as V}from"./vendor-mui-core-D7t4vMS5.js";import{al as J,x as ne,I as H,bW as oe,o as ae,k as ie,c as ce,n as le,q as ue,bX as de,L as me}from"./index-Ca33rdd_.js";import{b as he,a as j,m as X}from"./vendor-hooks-DAZ1H6Va.js";import{r as d}from"./vendor-react-DX7tU9aY.js";import{ab as pe,a2 as fe,B as ge,F as we,z as O}from"./vendor-utils-BzQg432x.js";import{u as xe}from"./launch-result-message-B2nhmkKt.js";import{W as be,a1 as ke,Q}from"./vendor-ux-Dl3wVbv_.js";import{g as Ue}from"./get-safe-url-D4zRnTpF.js";import{S as q}from"./button-B_Qd9K_t.js";import{S as ye}from"./layout-CydCBGqn.js";import{p as Re,P as Le,S as Se}from"./starting-progress-DC-J-5Os.js";import"./vendor-arcblock-ivFYo5vP.js";import"./content-layout-CanSZTqf.js";const ve=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}`},$e=t=>t?{"__did-connect__":ge.from(JSON.stringify({switchBehavior:"auto",forceConnected:t,showClose:!1}),"utf8").toString("base64")}:{},Be=(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 J.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 Be(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,$]=d.useState(),[g,B]=d.useState(null),{blocklet:o}=ne(),[P,C]=d.useState(!1),[p,K]=d.useState(""),L=H("fromLauncher"),G=H("launcherUrl"),Y=Ee(G),Z=$e(c?.userDid),w=!["starting","running"].includes(n),I=n==="starting",T={stopped:s("blocklet.start"),starting:s("blocklet.starting"),running:s("blocklet.open")},_=he(async()=>{const r=o?.controller?.launcherSessionId||o?.meta?.did,i=r?`/apps/${r}/overview`:"apps",u=await oe(Y);return we(O(u,"/u/",i),Z)});d.useEffect(()=>{_().then(r=>{B(r)}).catch(()=>{B(null)})},[_]);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(L&&g){window.location.href=g;return}C(!0);const r=await ee(),u=new URL(window.location.href).searchParams.get("redirect"),z=u&&r;if(!p||!ie(p)){Q.error(s("blocklet.gotoVisitError"));return}let U=p,v=[];if(z)try{const y=new URL(r);U=y.href,v=[y.hostname]}catch(y){console.error("Invalid redirect URL:",u,y)}const M=X.get("login_token"),W=X.get("vid"),f=new URL(U);f.host!==window.location.host&&M&&(f.pathname=O(ce.WELLKNOWN_SERVICE_PATH_PREFIX,"redirect-with-login"),f.searchParams.set("setupToken",M),W&&f.searchParams.set("visitorId",W),U=f.href),window.location.href=Ue(U,v.length?{allowDomains:v}:{}),C(!1)},S=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),$(r==="running"?null:s("setup.complete.notRunning"))}catch(r){const i=r?.response?.data||r.message;Q.error(i),h(!1),l("stopped"),$(i)}};d.useEffect(()=>{l(o.status),["starting","running"].includes(o.status)||S()},[]),d.useEffect(()=>{p||(async()=>{const i=await J.evaluateURLs(o.site.domainAliases.map(u=>de(u.value,me)),{preferAccessible:!0});i.length>0&&K(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=I||!p;L&&!F&&(w?S():A());const te=ke(Re,w?{ml:3}:{},I?{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")}`:T[n]||T.stopped,N=e.jsx(Le,{blocklets:o.children??[],status:n,debounce:!1,children:e.jsx(q,{sx:te,onClick:()=>w?S():A(),loading:F||P,children:e.jsx("span",{style:{position:"relative",zIndex:2},children:se()})})});return e.jsx(Ie,{children:e.jsx(ve,{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(Se,{blocklets:o.children??[]}):null]}),footer:e.jsxs(Te,{children:[w&&e.jsx(q,{variant:"outlined",disabled:R,onClick:()=>t(),children:s("setup.previous")}),L?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 Ie=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 Te=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};
