import { format, isToday, isYesterday, parseISO } from "date-fns";

function groupNotifications(notifications: any[]) {
  const grouped = {
    Today: [],
    Yesterday: [],
    Other: {} as Record<string, any[]>
  };

  notifications.forEach(notification => {
    const date = parseISO(notification.created_at);

    if (isToday(date)) {
      grouped.Today.push(notification);
    } else if (isYesterday(date)) {
      grouped.Yesterday.push(notification);
    } else {
      const formattedDate = format(date, "dd MMMM, yyyy");
      if (!grouped.Other[formattedDate]) {
        grouped.Other[formattedDate] = [];
      }
      grouped.Other[formattedDate].push(notification);
    }
  });

  return grouped;
}

export default groupNotifications;
