import type { ChatAPIResponse } from '$lib/types/api/ChatAPIResponse'; import type { MatchChatResponse, Message, Match } from '$lib/types'; /** * Transform raw chat API response into structured format * @param rawData - Raw API response * @param matchId - Match ID * @param match - Match data with player information * @returns Structured chat data */ export function transformChatResponse( rawData: ChatAPIResponse, matchId: string, match?: Match ): MatchChatResponse { const messages: Message[] = []; // Create player ID to name mapping const playerMap = new Map(); if (match?.players) { for (const player of match.players) { playerMap.set(player.id, player.name); } } // Flatten all player messages into a single array for (const [playerId, playerMessages] of Object.entries(rawData)) { const playerName = playerMap.get(playerId) || `Player ${playerId}`; for (const message of playerMessages) { messages.push({ ...message, player_id: Number(playerId), player_name: playerName }); } } // Sort by tick messages.sort((a, b) => a.tick - b.tick); return { match_id: matchId, messages }; }