import type { RoomSummary } from './types';

export interface PaneBadge {
  count: number;
  mention: boolean;
}

export interface PaneBadges {
  rail: PaneBadge;
  channels: PaneBadge;
}

/** DMs and mentions, each counted on whichever folded pane reaches its room in the fewest clicks. */
export function paneBadges(
  rooms: RoomSummary[],
  channelIds: Set<string>,
  openRoomId: string | null,
  muted: (room: RoomSummary) => boolean
): PaneBadges {
  const rail = { count: 0, mention: false };
  const channels = { count: 0, mention: false };
  for (const room of rooms) {
    if (room.space || room.membership !== 'Joined' || room.room_id === openRoomId || muted(room)) continue;
    const count = room.direct
      ? room.notification_mode === 'MentionsAndKeywordsOnly' ? room.unread_mentions : room.unread_messages + room.unread_mentions
      : room.unread_mentions;
    if (count === 0) continue;
    const badge = channelIds.has(room.room_id) ? channels : rail;
    badge.count += count;
    badge.mention ||= room.unread_mentions > 0;
  }
  return { rail, channels };
}
