import type { InternalMatchCompetition } from '@lolympus/contracts'; import type { InternalMatchCardView } from './community-stats.types'; /** Attach official identities without merging rematches between the same teams. */ export function presentStatsCard( card: InternalMatchCardView, competition?: Pick, ): InternalMatchCardView { if (!competition) return card; const officialGames = competition.projection.games ?? []; const games = card.games.map((game) => { const official = officialGames.find( (candidate) => candidate.displayNumber === game.gameNumber && (candidate.evidence.type === 'riot' ? candidate.evidence.riotMatchId : candidate.evidence.legacyReference) === game.riotMatchId, ); return official ? { ...game, seriesId: official.seriesId, ordinal: official.ordinal } : game; }); const visible = new Set(games.map((game) => game.seriesId)); return { ...card, games, teams: card.teams?.map((team) => ({ ...team, teamId: competition.teams.find((t) => t.displayOrder === team.teamIndex) ?.teamId, })), series: (competition.projection.series ?? []).filter( (series) => visible.has(series.seriesId) || (series.status === 'completed' && series.outcome.type !== 'played'), ), }; } /** Date-only inputs are inclusive Korean calendar days; ISO timestamps remain supported. */ export function statsDate( value: string | undefined, end: boolean, ): Date | null { if (!value) return null; if (/^\d{4}-\d{2}-\d{2}$/.test(value)) { const date = new Date( `${value}T${end ? '23:59:59.999' : '00:00:00.000'}+09:00`, ); const midnight = new Date(`${value}T00:00:00.000Z`); if ( !Number.isFinite(midnight.getTime()) || midnight.toISOString().slice(0, 10) !== value ) return new Date(NaN); return date; } return new Date(value); }