export type StackCounts = ReadonlyMap export function createStackCounts(items: readonly T[]): StackCounts { const counts = new Map() items.forEach((item) => counts.set(item, (counts.get(item) ?? 0) + 1)) return counts } export function stackCount(counts: StackCounts, id: T) { return counts.get(id) ?? 0 } export function summarizeStackCounts( counts: StackCounts, catalog: readonly TChoice[], emptyLabel = '', ) { const summary = Array.from(counts.entries()) .map(([id, count]) => { const label = catalog.find((choice) => choice.id === id)?.name ?? id return count > 1 ? `${label} x${count}` : label }) .join(', ') return summary || emptyLabel }