import type { APIRoute } from "astro";
import { getCollection } from "astro:content";

export const prerender = true;

const slugOf = (entry: { id: string }) => String(entry.id).replace(/\.(md|mdx)$/i, "");

export const GET: APIRoute = async () => {
  const entries = await getCollection("glossaire");

  const index = entries.map((e) => {
    const slug = slugOf(e);
    return {
      slug,
      term: e.data.term,
      aliases: e.data.aliases ?? [],
      definitionShort: e.data.definitionShort,
      href: `/glossaire/${slug}/`,
    };
  });

  return new Response(JSON.stringify(index), {
    headers: {
      "Content-Type": "application/json; charset=utf-8",
      "Cache-Control": "public, max-age=3600",
    },
  });
};