import{u as z,r as l,t as a,j as s,$ as D,i as L,f as H,g as M,e as P,aU as C,n as b,c as W,bw as U}from"./index-iQt3bUih.js";import{r as V,G as Y}from"./GuildDetailModal-CJMSaXWi.js";import{u as Z}from"./gameData-CvtDsga6.js";function J({client:r,instanceId:i,onShowOnMap:N}){z();const A=Z(),[x,F]=l.useState(null),[c,E]=l.useState(null),[I,O]=l.useState(null),[o,h]=l.useState(null),[v,p]=l.useState(!1),[d,w]=l.useState(!1),[y,f]=l.useState(null),[S,g]=l.useState(null),[j,R]=l.useState(""),k=j===""?null:Math.max(1,j),T=e=>k===null||e.members.length===0||e.bases.length===0?!1:!e.members.some(t=>t.lastOnlineDaysAgo!=null&&t.lastOnlineDaysAgo<k),G=e=>{const t=e.members.map(n=>n.lastOnlineDaysAgo).filter(n=>n!=null);return t.length?Math.min(...t):Number.POSITIVE_INFINITY},u=l.useCallback(async()=>{try{const e=await r.guildsSnapshot(i);F(e.worldGuid),E(e.generatedAt),O(e.guilds),h(null);try{const t=await r.saveHealth(i,e.worldGuid);p(t.supported),t.supported||h(t.reason??a("此主機不支援存檔掃描"))}catch{p(!1)}}catch(e){p(!1),h(a("無法取得存檔快照:{reason}",{reason:e instanceof Error?e.message:String(e)}))}},[r,i]);l.useEffect(()=>{u()},[u]);const $=async()=>{if(x){f(null),w(!0);try{await r.startSaveHealth(i,x),await new Promise(e=>{const t=setInterval(async()=>{try{const n=await r.saveHealth(i,x);n.phase==="idle"&&(clearInterval(t),n.error&&f(n.error),e())}catch{}},2e3)}),await u()}catch(e){f(e instanceof Error?e.message:String(e))}finally{w(!1)}}},m=[...I??[]].sort((e,t)=>{const n=t.bases.length-e.bases.length;return n!==0?n:G(e)-G(t)});return s.jsxs("div",{className:"guild-operations-board flex flex-col gap-4",children:[s.jsxs("section",{className:"guild-board-intro",children:[s.jsxs("div",{children:[s.jsx("span",{className:"command-kicker",children:"WORLD ORGANIZATION"}),s.jsx("h2",{children:"公會作戰板"})]}),s.jsx("p",{children:"用基地規模與最後活動時間找出活躍公會、閒置資產與需要關注的組織。"}),s.jsxs("div",{className:"guild-board-count",children:[s.jsx("strong",{children:m.length}),s.jsx("span",{children:"個公會"})]})]}),(c||!o||v)&&s.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-2",children:[s.jsx("p",{className:"text-xs text-ink-muted",children:c?a("資料來自存檔掃描(掃描於 {when})。",{when:new Date(c).toLocaleString()}):o?"":a("尚未掃描過存檔。點「從存檔刷新」建立快照。")}),m.length>0&&s.jsxs("label",{className:"inline-flex items-center gap-1.5 text-xs text-ink-muted",children:[s.jsx(D,{className:"size-3.5 shrink-0 text-amber-500"}),a("無人公會:標出"),s.jsx("input",{type:"number",min:1,value:j,onChange:e=>R(e.target.value===""?"":Math.max(1,Number(e.target.value))),placeholder:"30",className:`${L} w-20 px-2 py-1`}),a("日內無任何公會成員上線")]}),v&&s.jsxs("button",{className:`${H} inline-flex items-center gap-1.5`,onClick:()=>void $(),disabled:d,children:[s.jsx(M,{className:`size-3.5 ${d?"animate-spin":""}`}),d?a("掃描存檔中…(依存檔大小可能需要幾分鐘)"):a("從存檔刷新")]})]}),y&&s.jsx("p",{className:P,children:y}),o&&!d&&s.jsx(C,{icon:s.jsx(b,{}),children:o}),!o&&!d&&m.length===0&&s.jsx(C,{icon:s.jsx(b,{}),children:c?a("這個世界還沒有公會。"):a("尚未掃描過存檔。點「從存檔刷新」建立快照。")}),s.jsx("div",{className:"guild-card-matrix",children:m.map(e=>{var n;const t=T(e);return s.jsxs("button",{className:`${W} guild-operation-card flex w-full flex-wrap items-center justify-between gap-3 text-left transition hover:border-pal/50 ${t?"!border-red-500/70 bg-red-500/5":""}`,onClick:()=>g(e),children:[s.jsxs("div",{className:"min-w-0",children:[s.jsxs("p",{className:"flex items-center gap-2 text-sm font-extrabold",children:[t?s.jsx(D,{className:"size-4 shrink-0 text-red-500"}):s.jsx(b,{className:"size-4 shrink-0 text-pal"}),s.jsx("span",{className:"truncate",children:e.name}),e.baseCampLevel!==null&&s.jsx("span",{className:"rounded-full bg-card-soft px-2 py-0.5 text-xs font-bold text-ink-muted",children:a("據點等級 Lv.{n}",{n:e.baseCampLevel})})]}),s.jsxs("p",{className:"mt-1 text-[13px] text-ink-muted",children:[a("{n} 名成員",{n:e.members.length})," · ",a("{n} 個據點",{n:e.bases.length}),t&&s.jsxs(s.Fragment,{children:[" · ",s.jsx("span",{className:"font-bold text-red-500",children:a("無人公會")})]}),e.storage!==null&&s.jsxs(s.Fragment,{children:[" · ",a("倉庫 {n} 種物品",{n:e.storage.length})]}),((n=e.research)==null?void 0:n.currentId)&&s.jsxs(s.Fragment,{children:[" · ",a("研究中:{id}",{id:V(A,e.research.currentId)})]})]})]}),s.jsx(U,{className:"size-4 shrink-0 text-ink-muted"})]},e.id)})}),S&&s.jsx(Y,{client:r,instanceId:i,guild:S,generatedAt:c,onRescanned:()=>void u(),onShowOnMap:N?(e,t)=>{g(null),N(e,t)}:void 0,onClose:()=>g(null)})]})}export{J as GuildsTab};
