// ─── Formatting Functions ─────────────────────────────────────────────────────

import { ELEMENTS } from '@/lib/game/constants';
import type { SpellCost } from '@/lib/game/types';

export function fmt(n: number): string {
  if (!isFinite(n) || isNaN(n)) return '0';
  if (n >= 1e9) return (n / 1e9).toFixed(2) + 'B';
  if (n >= 1e6) return (n / 1e6).toFixed(2) + 'M';
  if (n >= 1e3) return (n / 1e3).toFixed(1) + 'K';
  return Math.floor(n).toString();
}

export function fmtDec(n: number, d: number = 1): string {
  return isFinite(n) ? n.toFixed(d) : '0';
}

/**
 * Format a spell cost for display
 */
export function formatSpellCost(cost: SpellCost): string {
  if (cost.type === 'raw') {
    return `${cost.amount} raw`;
  }
  const elemDef = ELEMENTS[cost.element || ''];
  return `${cost.amount} ${elemDef?.sym || '?'}`;
}

/**
 * Get the display color for a spell cost
 */
export function getSpellCostColor(cost: SpellCost): string {
  if (cost.type === 'raw') {
    return '#60A5FA'; // Blue for raw mana
  }
  return ELEMENTS[cost.element || '']?.color || '#9CA3AF';
}

/**
 * Format study time in hours to human-readable string
 */
export function formatStudyTime(hours: number): string {
  if (hours < 1) return `${Math.round(hours * 60)}m`;
  return `${hours.toFixed(1)}h`;
}

/**
 * Format time (hour of day) to HH:MM format
 */
export function formatHour(hour: number): string {
  const h = Math.floor(hour);
  const m = Math.floor((hour % 1) * 60);
  return `${h.toString().padStart(2, '0')}:${m.toString().padStart(2, '0')}`;
}
