import type { CombatLogEntry } from '../game'

export type BasicFloatingCombatText = {
  id: number
  memberId: string
  value: number
}

export const DEFAULT_COMBAT_LOG_LIMIT = 60
export const STADIUM_COMBAT_LOG_LIMIT = 70
export const DEFAULT_FLOATING_TEXT_LIMIT = 48

export function appendCombatLog(
  current: CombatLogEntry[],
  entry: CombatLogEntry,
  limit = DEFAULT_COMBAT_LOG_LIMIT,
) {
  return [entry, ...current].slice(0, limit)
}

export function appendFloatingText<T extends BasicFloatingCombatText>(
  current: T[],
  entry: T,
  limit = DEFAULT_FLOATING_TEXT_LIMIT,
) {
  return [...current, entry].slice(-limit)
}

export function groupFloatingTextsByMember<T extends BasicFloatingCombatText>(texts: T[]) {
  const groups = new Map<string, T[]>()
  texts.forEach((entry) => {
    const current = groups.get(entry.memberId)
    if (current) current.push(entry)
    else groups.set(entry.memberId, [entry])
  })
  return groups
}

export function stripFloatingTextSide<T extends BasicFloatingCombatText>(texts: T[]): BasicFloatingCombatText[] {
  return texts.map(({ id, memberId, value }) => ({ id, memberId, value }))
}
