import { getCollection, type CollectionEntry } from "astro:content";

// Drafts are excluded from production builds; visible in dev for preview.
export async function getPosts(): Promise<CollectionEntry<"blog">[]> {
  const posts = await getCollection("blog", ({ data }) =>
    import.meta.env.PROD ? !data.draft : true,
  );
  return posts.sort((a, b) => b.data.date.valueOf() - a.data.date.valueOf());
}

export function allTags(posts: CollectionEntry<"blog">[]): string[] {
  return [...new Set(posts.flatMap((p) => p.data.tags))].sort();
}

export function formatDate(d: Date): string {
  return d.toLocaleDateString("en-AU", {
    year: "numeric",
    month: "short",
    day: "numeric",
  });
}
