---
import PostList from "~/components/astro/post-list.astro"
import { en, zh } from "~/config"
import { getLangFromUrl } from "~/i18n/utils"
import MainLayout from "~/layouts/main.astro"
import { getPostsByLocale } from "~/utils"
import { getLanguagePaths } from "~/utils/langs"

const lang = getLangFromUrl(Astro.url)
const pageMeta = lang === "zh" ? zh.pageMeta : en.pageMeta

export function getStaticPaths() {
  return getLanguagePaths()
}

const posts = await getPostsByLocale(lang)

const postsByYear = posts.reduce(
  (acc: Record<string, any[]>, post: any) => {
    const year = new Date(post.data.pubDate).getFullYear().toString()
    if (!acc[year]) {
      acc[year] = []
    }
    acc[year].push(post)
    return acc
  },
  {} as Record<string, any[]>,
)

const years = Object.keys(postsByYear).sort((a, b) => Number(b) - Number(a))
---

<MainLayout
  title={pageMeta.archive.title}
  description={pageMeta.archive.description}
  ogImage={pageMeta.archive.ogImage}
>
  {
    years.map((year) => (
      <div class="year-group my-8">
        <h2 class="my-4 text-2xl font-bold">{year}</h2>
        {postsByYear[year].map((post: any) => (
          <PostList
            post={post}
            lang={lang}
            dateFormat="MM-DD"
            dateWidth="w-20"
          />
        ))}
      </div>
    ))
  }
</MainLayout>
