import { useMemo } from "react";
import type { Post } from "../../../types/post";

export type DayGroup = {
  dayKey: string;
  dayLabel: string;
  items: Post[];
};

function localeFor(lang: string): string {
  const locales: Record<string, string> = {
    zh: "zh-CN",
    en: "en-US",
    ja: "ja-JP",
    ko: "ko-KR",
    vi: "vi-VN",
    id: "id-ID",
    ms: "ms-MY",
  };
  return locales[lang] ?? "en-US";
}

function dayKey(iso: string): string {
  const d = new Date(iso);
  return `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()}`;
}

function dayLabel(iso: string, lang: string): string {
  const d = new Date(iso);
  const today = new Date();
  const yesterday = new Date();
  yesterday.setDate(today.getDate() - 1);
  const isSameDay = (a: Date, b: Date) =>
    a.getFullYear() === b.getFullYear() &&
    a.getMonth() === b.getMonth() &&
    a.getDate() === b.getDate();
  if (isSameDay(d, today)) {
    return lang === "zh-CN" ? "今天" : "Today";
  }
  if (isSameDay(d, yesterday)) {
    return lang === "zh-CN" ? "昨天" : "Yesterday";
  }
  return new Intl.DateTimeFormat(localeFor(lang), {
    month: "long",
    day: "numeric",
  }).format(d);
}

export function useGroupedByDay(posts: Post[], lang: string): DayGroup[] {
  return useMemo(() => {
    const groups: DayGroup[] = [];
    const seen = new Map<string, DayGroup>();
    for (const p of posts) {
      const k = dayKey(p.publishedAt);
      let g = seen.get(k);
      if (!g) {
        g = { dayKey: k, dayLabel: dayLabel(p.publishedAt, lang), items: [] };
        seen.set(k, g);
        groups.push(g);
      }
      g.items.push(p);
    }
    return groups;
  }, [posts, lang]);
}

export { dayKey as _dayKey, dayLabel as _dayLabel };
