import{at as E,j as e,ak as m,W as V}from"./vendor-mui-core-CKKG_1TN.js";import{aA as X,y as ne,K as H,cw as oe,q as ae,l as ie,cx as O,c as ce,o as le,r as ue,cy as de,O as me}from"./index-BEfIUbZC.js";import{r as d}from"./vendor-react-CTtPmzVP.js";import{ae as he,a6 as pe,B as fe,F as ge,z as q}from"./vendor-utils-CUs2ebe7.js";import{b as we,a as j}from"./vendor-hooks-DQAdyHrE.js";import{u as xe}from"./launch-result-message-C04PaKk0.js";import{V as be,a3 as ke,W as K}from"./vendor-ux-DjXlxcvL.js";import{g as ye}from"./get-safe-url-DxqVk96E.js";import{S as Q}from"./button-LwkniwV9.js";import{S as Ue}from"./layout-DRVZ4L0D.js";import{p as Re,P as Se,S as Le}from"./starting-progress-BiI8hmaj.js";import"./vendor-arcblock-BCP2x3BR.js";import"./lottie-web-4koyQiv_.js";import"./content-layout-Dtfb5X6M.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}=he(pe(t));return`${c}//${s}`},$e=t=>t?{"__did-connect__":fe.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 X.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,J]=d.useState(""),S=H("fromLauncher"),G=H("launcherUrl"),Y=Ee(G),Z=$e(c?.userDid),w=!["starting","running"].includes(n),_=n==="starting",I={stopped:s("blocklet.start"),starting:s("blocklet.starting"),running:s("blocklet.open")},T=we(async()=>{const r=o?.controller?.launcherSessionId||o?.meta?.did,i=r?`/apps/${r}/overview`:"apps",u=await oe(Y);return ge(q(u,"/u/",i),Z)});d.useEffect(()=>{T().then(r=>{B(r)}).catch(()=>{B(null)})},[T]);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"),z=u&&r;if(!p||!ie(p)){K.error(s("blocklet.gotoVisitError"));return}let y=p,v=[];if(z)try{const U=new URL(r);y=U.href,v=[U.hostname]}catch(U){console.error("Invalid redirect URL:",u,U)}const M=O.get("login_token"),W=O.get("vid"),f=new URL(y);f.host!==window.location.host&&M&&(f.pathname=q(ce.WELLKNOWN_SERVICE_PATH_PREFIX,"redirect-with-login"),f.searchParams.set("setupToken",M),W&&f.searchParams.set("visitorId",W),y=f.href),window.location.href=ye(y,v.length?{allowDomains:v}:{}),C(!1)},L=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;K.error(i),h(!1),l("stopped"),$(i)}};d.useEffect(()=>{l(o.status),["starting","running"].includes(o.status)||L()},[]),d.useEffect(()=>{p||(async()=>{const i=await X.evaluateURLs(o.site.domainAliases.map(u=>de(u.value,me)),{preferAccessible:!0});i.length>0&&J(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=_||!p;S&&!F&&(w?L():A());const te=ke(Re,w?{ml:3}:{},_?{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")}`:I[n]||I.stopped,N=e.jsx(Se,{blocklets:o.children??[],status:n,debounce:!1,children:e.jsx(Q,{sx:te,onClick:()=>w?L():A(),loading:F||P,children:e.jsx("span",{style:{position:"relative",zIndex:2},children:se()})})});return e.jsx(_e,{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(Le,{blocklets:o.children??[]}):null]}),footer:e.jsxs(Ie,{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 _e=E(Ue)`
  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 Ie=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};
