import{at as E,j as e,ak as m,W}from"./vendor-mui-core-CnXrAOs3.js";import{ad as q,l as ne,ah as H,aB as oe,e as ae,Z as ie,aC as O,c as ce,d as le,f as ue,aD as de,ak as me}from"./index-sNHCLgAE.js";import{r as d}from"./vendor-react-Dvs43sk9.js";import{V as he,O as pe,B as fe,J as ge,C as J}from"./vendor-utils-cJEl_UEd.js";import{b as we,a as j}from"./vendor-hooks-CjESGRlR.js";import{u as xe}from"./launch-result-message-DWeUg9rM.js";import{V as be,o as ke,H as Q}from"./vendor-ux-IiQnod3w.js";import{g as Ue}from"./get-safe-url-DpalOXcM.js";import{S as X}from"./button-vwR-NYG5.js";import{S as ye}from"./layout-Ccrs-yyw.js";import{p as Re,P as Se,S as Le}from"./starting-progress-NSYAzE2f.js";import"./vendor-arcblock-CuFtPveO.js";import"./lottie-web-4koyQiv_.js";import"./content-layout-oyz1EqNZ.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}`},Be=t=>t?{"__did-connect__":fe.from(JSON.stringify({switchBehavior:"auto",forceConnected:t,showClose:!1}),"utf8").toString("base64")}:{},Ce=(t=0)=>new Promise(c=>setTimeout(c,t));async function $e(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 Ce(1e3),n=await s("https"),n)return n;return n||null}const h=await R();return h?{accessible:!0,url:h}:{accessible:!1}}function Pe({onPrevious:t=()=>{},launcherSession:c={}}){const{t:s}=be(),[R,h]=d.useState(!1),[n,l]=d.useState("starting"),[a,B]=d.useState(),[g,C]=d.useState(null),{blocklet:o}=ne(),[$,P]=d.useState(!1),[p,K]=d.useState(""),S=H("fromLauncher"),Z=H("launcherUrl"),G=Ee(Z),Y=Be(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(G);return ge(J(u,"/u/",i),Y)});d.useEffect(()=>{T().then(r=>{C(r)}).catch(()=>{C(null)})},[T]);const ee=async()=>{try{const i=new URL(window.location.href).searchParams.get("redirect");if(!i)return null;const u=await $e(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}P(!0);const r=await ee(),u=new URL(window.location.href).searchParams.get("redirect"),M=u&&r;if(!p||!ie(p)){Q.error(s("blocklet.gotoVisitError"));return}let U=p,v=[];if(M)try{const y=new URL(r);U=y.href,v=[y.hostname]}catch(y){console.error("Invalid redirect URL:",u,y)}const V=O.get("login_token"),z=O.get("vid"),f=new URL(U);f.host!==window.location.host&&V&&(f.pathname=J(ce.WELLKNOWN_SERVICE_PATH_PREFIX,"redirect-with-login"),f.searchParams.set("setupToken",V),z&&f.searchParams.set("visitorId",z),U=f.href),window.location.href=Ue(U,v.length?{allowDomains:v}:{}),P(!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),B(r==="running"?null:s("setup.complete.notRunning"))}catch(r){const i=r?.response?.data||r.message;Q.error(i),h(!1),l("stopped"),B(i)}};d.useEffect(()=>{l(o.status),["starting","running"].includes(o.status)||L()},[]),d.useEffect(()=>{p||(async()=>{const i=await q.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"),N=_||!p;S&&!N&&(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=()=>$?s("setup.complete.opening"):n==="starting"&&!a?`(${b}/${x}) ${s("blocklet.starting")}`:I[n]||I.stopped,F=e.jsx(Se,{blocklets:o.children??[],status:n,debounce:!1,children:e.jsx(X,{sx:te,onClick:()=>w?L():A(),loading:N||$,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(X,{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:F}),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:F}),(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(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;
  }
`;Pe.propTypes={onPrevious:W.func,launcherSession:W.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{Pe as default};
