export function formatRON(value: number): string {
  if (!Number.isFinite(value) || value <= 0) return '0';
  if (value >= 1_000_000_000) return `${(value / 1_000_000_000).toFixed(1)} mld`;
  if (value >= 1_000_000) return `${(value / 1_000_000).toFixed(1)} mil`;
  if (value >= 1_000) return `${(value / 1_000).toFixed(0)}K`;
  return Math.round(value).toString();
}

export function formatCount(value: number): string {
  if (!Number.isFinite(value)) return '0';
  return new Intl.NumberFormat('ro-RO').format(Math.round(value));
}

export function formatDate(dateStr: string): string {
  return new Date(dateStr).toLocaleDateString('ro-RO', {
    day: '2-digit',
    month: 'short',
    year: 'numeric',
  });
}

export function timeAgo(dateStr: string): string {
  const date = new Date(dateStr);
  const now = new Date();
  const diffMs = now.getTime() - date.getTime();
  const diffMin = Math.floor(diffMs / 60_000);
  const diffHrs = Math.floor(diffMs / 3_600_000);
  const diffDays = Math.floor(diffMs / 86_400_000);

  if (diffMin < 1) return 'acum';
  if (diffMin < 60) return `acum ${diffMin}m`;
  if (diffHrs < 24) return `acum ${diffHrs}h`;
  if (diffDays < 30) return `acum ${diffDays}z`;
  return date.toLocaleDateString('ro-RO', { day: '2-digit', month: 'short' });
}

export function diacritic(str: string): string {
  return str
    .normalize('NFD')
    .replace(/[̀-ͯ]/g, '')
    .toLowerCase();
}
