/**
 * Date/time utilities — @zync/utils.
 *
 * formatRelativeTime: converts a Date to a human-readable Hebrew string.
 * Uses Intl.RelativeTimeFormat for < 7 days, Intl.DateTimeFormat for >= 7 days.
 */

/**
 * Formats a past date as a Hebrew relative time string.
 *
 * < 60s  → "עכשיו"
 * < 60m  → "לפני N דקות"
 * < 24h  → "לפני N שעות"
 * < 7d   → "לפני N ימים"
 * >= 7d  → absolute Hebrew long date e.g. "12 ביוני 2026"
 */
export function formatRelativeTime(date: Date): string {
  const now = Date.now()
  const diffMs = now - date.getTime()
  const sec = Math.floor(diffMs / 1000)
  const min = Math.floor(sec / 60)
  const hr = Math.floor(min / 60)
  const day = Math.floor(hr / 24)

  if (sec < 60) return 'עכשיו'

  const rtf = new Intl.RelativeTimeFormat('he-IL', { numeric: 'always' })
  if (min < 60) return rtf.format(-min, 'minute') // "לפני N דקות"
  if (hr < 24) return rtf.format(-hr, 'hour') // "לפני N שעות"
  if (day < 7) return rtf.format(-day, 'day') // "לפני N ימים"

  return new Intl.DateTimeFormat('he-IL', {
    day: 'numeric',
    month: 'long',
    year: 'numeric',
  }).format(date) // "12 ביוני 2026"
}
