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); // Highlight guild performance, rather than filling the page with the newest // reports. Incomplete stats follow verified results; the full archive stays // available in the detailed table. const advantage=b=>b.kills!=null&&b.deaths!=null?b.kills-b.deaths:-Infinity; const featured=[...battles].sort((a,b)=>advantage(b)-advantage(a)||(b.kills??-1)-(a.kills??-1)||(b.date||'').localeCompare(a.date||'')).slice(0,6); // 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=featured.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)}
`:''}
`; }); return `
${number(battles.length)}${e(t.archivedReports)}
${number(engagements)}${e(t.loggedEngagements)}

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

${cards.join('')}
`; }