/**
 * Shared formatting utilities for room components.
 */

// ─── Message Time ─────────────────────────────────────────────────────────────

export function formatMessageTime(iso: string): string {
  const d = new Date(iso);
  const now = new Date();
  const isToday = d.toDateString() === now.toDateString();
  const yesterdayMs = now.getTime() - 86400000;
  const isYesterday = new Date(yesterdayMs).toDateString() === d.toDateString();

  if (isToday) {
    return d.toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit' });
  }
  if (isYesterday) {
    return `Yesterday ${d.toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit' })}`;
  }
  return d.toLocaleString(undefined, { dateStyle: 'short', timeStyle: 'short' });
}

// ─── Date Divider ─────────────────────────────────────────────────────────────

export function formatDateDivider(iso: string): string {
  const date = new Date(iso);
  const now = new Date();
  const today = now.toDateString();
  const yesterday = new Date(now.getTime() - 86400000).toDateString();
  const key = date.toDateString();

  if (key === today) return 'Today';
  if (key === yesterday) return 'Yesterday';

  return date.toLocaleDateString(undefined, {
    weekday: 'long',
    year: 'numeric',
    month: 'long',
    day: 'numeric',
  });
}
