import type { AbilityKey, AbilityScores } from './types';
import { ABILITY_KEYS } from './types';

/** Shared d20 ability modifier. Clamps absurd inputs rather than returning NaN. */
export function abilityModifier(score: number): number {
  if (!Number.isFinite(score)) return 0;
  return Math.floor((score - 10) / 2);
}

export function defaultAbilityScores(): AbilityScores {
  return { str: 10, dex: 10, con: 10, int: 10, wis: 10, cha: 10 };
}

/** Compose a damage expression from dice and a flat modifier, e.g. ("1d8", 3) -> "1d8+3". */
export function formatDamage(dice: string | undefined, mod: number): string {
  const base = dice && dice.trim() !== '' ? dice.trim() : '0';
  if (mod === 0) return base;
  return mod > 0 ? `${base}+${mod}` : `${base}${mod}`;
}

export const ABILITY_LABELS: Record<AbilityKey, string> = {
  str: 'Strength',
  dex: 'Dexterity',
  con: 'Constitution',
  int: 'Intelligence',
  wis: 'Wisdom',
  cha: 'Charisma',
};

export const ABILITY_ABBR: Record<AbilityKey, string> = {
  str: 'STR',
  dex: 'DEX',
  con: 'CON',
  int: 'INT',
  wis: 'WIS',
  cha: 'CHA',
};

export { ABILITY_KEYS };
