import {BadgeID, UserBadge} from 'types/badges';

export type BadgeGroup = {
    badge: UserBadge;
    count: number;
}

const MAX_DESC_LENGTH = 40;

export function groupBadges(badges: UserBadge[]): BadgeGroup[] {
    const map = new Map<BadgeID, BadgeGroup>();
    for (const badge of badges) {
        const existing = map.get(badge.id);
        if (existing) {
            existing.count++;
        } else {
            map.set(badge.id, {badge, count: 1});
        }
    }
    return Array.from(map.values());
}

export function truncateText(text: string): string {
    return text.length > MAX_DESC_LENGTH ? text.slice(0, MAX_DESC_LENGTH) + '...' : text;
}
