import{r as c,j as t}from"./index-iQt3bUih.js";import{a as l,b as j,M as E,T as N,I as P,e as b}from"./mapLayers-CfwBxAIe.js";import"./gameData-CvtDsga6.js";function $({shareId:m}){const d=c.useRef(null),u=c.useRef(null),[s,y]=c.useState(null),[r,h]=c.useState("world"),[x,w]=c.useState(null);if(c.useEffect(()=>{let n=!1;const o=async()=>{try{const a=await fetch(`/api/public-map/snapshot?id=${encodeURIComponent(m)}`),p=await a.json().catch(()=>({}));if(!a.ok)throw new Error(p.error||`HTTP ${a.status}`);n||(y(p),w(null))}catch(a){n||w(a instanceof Error?a.message:String(a))}};o();const i=window.setInterval(()=>void o(),3e4);return()=>{n=!0,window.clearInterval(i)}},[m]),c.useEffect(()=>{var p;if(!d.current||!s)return;(p=u.current)==null||p.remove();const n=r==="tree"?N:P,o=l.map(d.current,{crs:l.CRS.Simple,minZoom:-2,maxZoom:4,zoomControl:!0});u.current=o,l.imageOverlay(r==="tree"?j:E,n).addTo(o),o.fitBounds(n);const i=(e,f)=>l.divIcon({className:"",html:`<span style="display:grid;place-items:center;width:28px;height:28px;border-radius:999px;background:${e};color:white;border:2px solid white;box-shadow:0 2px 8px #0008;font-weight:900">${f}</span>`,iconSize:[28,28],iconAnchor:[14,14]}),a=s.snapshot;for(const e of a.players??[])e.m===r&&l.marker([e.y,e.x],{icon:i(e.warn?"#dc2626":"#0e9fba",e.warn?"!":"●")}).bindPopup(`<b>${b(e.n)}</b><br>Lv.${e.lv}`).addTo(o);for(const e of a.offline??[])e.m===r&&l.marker([e.y,e.x],{icon:i("#64748b","○")}).bindPopup(`<b>${b(e.n)}</b><br>離線 · Lv.${e.lv}`).addTo(o);for(const e of a.bases??[])e.m===r&&l.marker([e.y,e.x],{icon:i(e.c||"#7c3aed","⌂")}).bindPopup(b(e.g||"公會據點")).addTo(o);for(const e of a.bosses??[])if(e.m===r){const f=e.st==="alive"?"頭目可挑戰":e.ra?`預計 ${new Date(e.ra*1e3).toLocaleTimeString()} 重生`:"頭目重生中";l.marker([e.y,e.x],{icon:i(e.st==="alive"?"#f59e0b":"#334155","♛")}).bindPopup(f).addTo(o)}return()=>{o.remove(),u.current=null}},[s,r]),x&&!s)return t.jsx(g,{title:"無法開啟公開地圖",detail:x});if(!s)return t.jsx(g,{title:"正在載入公開地圖",detail:"首次啟用可能需要幾秒鐘產生快照。"});const v=[...s.snapshot.players??[],...s.snapshot.offline??[],...s.snapshot.bases??[],...s.snapshot.bosses??[]].some(n=>n.m==="tree");return t.jsxs("div",{className:"relative h-screen w-screen overflow-hidden bg-slate-950",children:[t.jsx("div",{ref:d,className:"h-full w-full"}),t.jsxs("section",{className:"absolute left-4 top-4 z-1000 max-w-[calc(100%-2rem)] rounded-2xl bg-slate-950/90 px-4 py-3 text-white shadow-xl backdrop-blur",children:[t.jsx("h1",{className:"font-black",children:s.snapshot.name}),t.jsxs("p",{className:"text-xs text-slate-300",children:["線上 ",s.snapshot.onlineCount,s.snapshot.maxPlayers?` / ${s.snapshot.maxPlayers}`:""," · 更新 ",new Date(s.updatedAt).toLocaleString()]}),v&&t.jsxs("div",{className:"mt-2 flex gap-2",children:[t.jsx("button",{className:r==="world"?"rounded-lg bg-cyan-600 px-3 py-1 text-xs font-bold":"rounded-lg bg-slate-700 px-3 py-1 text-xs",onClick:()=>h("world"),children:"主世界"}),t.jsx("button",{className:r==="tree"?"rounded-lg bg-cyan-600 px-3 py-1 text-xs font-bold":"rounded-lg bg-slate-700 px-3 py-1 text-xs",onClick:()=>h("tree"),children:"世界樹"})]}),x&&t.jsxs("p",{className:"mt-1 text-xs text-amber-300",children:["更新暫時失敗，正顯示上次快照：",x]})]})]})}function g({title:m,detail:d}){return t.jsx("main",{className:"grid min-h-screen place-items-center bg-slate-950 p-6 text-white",children:t.jsxs("div",{className:"max-w-md text-center",children:[t.jsx("h1",{className:"text-xl font-black",children:m}),t.jsx("p",{className:"mt-2 text-sm text-slate-300",children:d})]})})}export{$ as PublicMapPage};
