import { escapeHtml as e, localized } from './lib.mjs'; import { directPreview } from './battle-previews.mjs'; export function battleDetails(url) { const u=new URL(url); const combined=/\/battles\/multi\/?$/.test(u.pathname); const ids=combined?[...new Set((u.searchParams.get('ids')||'').split(',').filter(id=>/^\d+$/.test(id)))]:[u.pathname.split('/').filter(Boolean).pop()].filter(id=>/^\d+$/.test(id)); return {combined,ids,region:u.hostname==='europe.albionbb.com'?'europe':u.hostname}; } export function renderBattleCards({battles,previews,lang,base,icon,t}) { if (!battles.length) return ''; const number=n=>new Intl.NumberFormat(lang==='fa'?'fa-IR':'en-US').format(n); const sorted=[...battles].sort((a,b)=>(b.date||'').localeCompare(a.date||'')); // Combined reports may overlap with single reports; count unique, explicit battle IDs. const engagements=new Set(battles.flatMap(b=>battleDetails(b.url).ids)).size; const cards=sorted.map((battle,index)=>{ const info=battleDetails(battle.url); const preview=previews[battle.url]||battle.preview||directPreview(battle.url); const image=preview?`${e(localized(battle.title,lang))} — AlbionBB`:''; const outcome=battle.outcome||'recorded'; const date=battle.date?``:e(t.battleArchive); return `
${image}
${icon(info.combined?'shield':'swords')}${e(info.combined?t.combinedReport:t.singleReport)}${e(info.region==='europe'?t.europe:info.region)}
${number(info.ids.length||1)}${e(info.ids.length===1?t.engagement:t.engagements)}
${date}${e(t[outcome])}

${e(localized(battle.title,lang))}

${info.ids.map(id=>`#${e(id)}`).join('')}
${battle.kills!=null||battle.deaths!=null?`
${e(t.kills)}
${battle.kills==null?'—':number(battle.kills)}
${e(t.deaths)}
${battle.deaths==null?'—':number(battle.deaths)}
`:''}
`; }).join(''); return `
${number(battles.length)}${e(t.archivedReports)}
${number(engagements)}${e(t.loggedEngagements)}

${icon('shield')}${e(t.archiveMotto)}

${cards}
`; }