import { isCoolRouteId, isCoolScene, isCoolTime } from '../../shared/cool' import type { CoolDemoState, CoolRouteId, CoolRound, CoolVoteCounts, CoolVoteTally, } from '../../shared/cool' type StatePatch = Partial> const states = new Map() const votes = new Map>() const counts = new Map() let updatedAt = new Map() function blankState(round: CoolRound): CoolDemoState { return { round, time: '10:00', selectedRoute: 'shortest', scene: 'ruth', voteOpen: false, updatedAt: new Date().toISOString(), } } function ensureVotes(round: CoolRound): Map { let map = votes.get(round) if (!map) { map = new Map() votes.set(round, map) } return map } function ensureCounts(round: CoolRound): CoolVoteCounts { let value = counts.get(round) if (!value) { value = { shortest: 0, shaded: 0, accessible: 0 } counts.set(round, value) } return value } export function getCoolState(round: CoolRound): CoolDemoState { let state = states.get(round) if (!state) { state = blankState(round) states.set(round, state) } return { ...state } } export function updateCoolState(round: CoolRound, patch: StatePatch): CoolDemoState { const current = getCoolState(round) const next: CoolDemoState = { ...current, ...(isCoolTime(patch.time) ? { time: patch.time } : {}), ...(isCoolRouteId(patch.selectedRoute) ? { selectedRoute: patch.selectedRoute } : {}), ...(isCoolScene(patch.scene) ? { scene: patch.scene } : {}), ...(typeof patch.voteOpen === 'boolean' ? { voteOpen: patch.voteOpen } : {}), updatedAt: new Date().toISOString(), } states.set(round, next) return { ...next } } export function resetCoolRound(round: CoolRound): CoolDemoState { const next = blankState(round) states.set(round, next) votes.set(round, new Map()) counts.set(round, { shortest: 0, shaded: 0, accessible: 0 }) updatedAt = new Map([...updatedAt].filter(([key]) => key !== round)) return { ...next } } export function getCoolVoteTally(round: CoolRound): CoolVoteTally { return { round, counts: { ...ensureCounts(round) }, total: ensureVotes(round).size, updatedAt: updatedAt.get(round) ?? null, } } export function recordCoolVote( round: CoolRound, route: CoolRouteId, sessionId: string, ): { duplicate: boolean; route: CoolRouteId; tally: CoolVoteTally } { const state = getCoolState(round) if (!state.voteOpen) { throw new Error('Voting is not open') } const roundVotes = ensureVotes(round) const existing = roundVotes.get(sessionId) if (existing) { return { duplicate: true, route: existing, tally: getCoolVoteTally(round) } } if (!isCoolRouteId(route)) throw new Error(`Invalid route: ${route}`) roundVotes.set(sessionId, route) const current = ensureCounts(round) counts.set(round, { ...current, [route]: current[route] + 1 }) updatedAt.set(round, new Date().toISOString()) return { duplicate: false, route, tally: getCoolVoteTally(round) } } export function isValidCoolSessionId(value: unknown): value is string { return typeof value === 'string' && /^[A-Za-z0-9_-]{8,80}$/.test(value) } /** Test helper only. */ export function resetCoolDemoStore(): void { states.clear() votes.clear() counts.clear() updatedAt = new Map() }