export interface RelativeTimeLabels {
  justNow: string;
  minutesAgo: string;
  hoursAgo: string;
  daysAgo: string;
}

/**
 * Format a date string as a relative time label.
 * Pass labels from the active locale (e.g. `t.audit.relativeTime`); the
 * `{n}` placeholder is substituted with the actual count.
 */
export function formatRelativeTime(
  dateString: string,
  labels: RelativeTimeLabels,
): string {
  const diff = Date.now() - new Date(dateString).getTime();
  const minutes = Math.floor(diff / 60_000);
  const hours = Math.floor(diff / 3_600_000);
  const days = Math.floor(diff / 86_400_000);

  if (minutes < 1) return labels.justNow;
  if (minutes < 60) return labels.minutesAgo.replace('{n}', String(minutes));
  if (hours < 24) return labels.hoursAgo.replace('{n}', String(hours));
  return labels.daysAgo.replace('{n}', String(days));
}
