const s=document.querySelector("#connect-form"),a=document.querySelector("#connect-status");async function c(i,n){const e=await fetch(i,{method:n===void 0?"GET":"POST",headers:{"x-yoworlds":"1","x-pocketbeyond":"1",...n===void 0?{}:{"content-type":"application/json"}},...n===void 0?{}:{body:JSON.stringify(n)}}),t=await e.json();if(!e.ok)throw new Error(t.error??"Please retry.");return t}async function l(){try{const i=await c("/api/my-worlds"),n=document.querySelector("#my-worlds");n.replaceChildren();for(const e of i){const t=document.createElement("p");if(t.textContent=`${e.world.name} · ${e.world.id} · ${e.state} `,e.state==="pending"){const o=document.createElement("button");o.className="button secondary",o.textContent="Verify & connect",o.onclick=async()=>{o.disabled=!0;try{await c(`/api/worlds/${e.world.id}/verify`,{}),a.textContent="Your world is connected. It now appears in the network.",await l()}catch(r){a.textContent=r.message}finally{o.disabled=!1}},t.append(o)}n.append(t)}}catch{a.textContent="Save an account or sign in on the Explore page first, then return here.",s.hidden=!0}}s.onsubmit=i=>{i.preventDefault();const n=s.querySelector("button");n.disabled=!0,(async()=>{try{const e=await c("/api/worlds/register",Object.fromEntries(new FormData(s)));a.textContent="Registration reserved. Save your private server key, then host the verification file. The key is shown only here.";const t=document.querySelector("#world-setup");t.hidden=!1,t.querySelector("#world-environment").textContent=`CENTRAL_URL=${location.origin}
WORLD_ID=${e.world.id}
WORLD_URL=${e.world.url}
WORLD_SECRET=${e.serverKey}
PORT=3003
BIND_HOST=127.0.0.1`,t.querySelector("#world-verification").textContent=JSON.stringify(e.verification,null,2);const o=t.querySelector("#download-verification"),r=o.getAttribute("href");r!=null&&r.startsWith("blob:")&&URL.revokeObjectURL(r),o.href=URL.createObjectURL(new Blob([JSON.stringify(e.verification,null,2)],{type:"application/json"})),s.reset(),await l()}catch(e){a.textContent=e.message}finally{n.disabled=!1}})()};l();
