import { getCollection } from 'astro:content';
import { SITE_URL } from '../consts';

export const GET = async () => {
  const [posts, pages] = await Promise.all([
    getCollection('posts'),
    getCollection('pages'),
  ]);
  const urls = [
    `${SITE_URL}/`,
    `${SITE_URL}/news/`,
    `${SITE_URL}/privacy/`,
  ];
  for (const p of pages) {
    if (p.data.slug === 'forum') continue;
    urls.push(`${SITE_URL}/${p.data.slug}/`);
  }
  for (const p of posts) {
    urls.push(`${SITE_URL}/${p.data.slug}/`);
  }
  // categories
  const cats = new Set<string>();
  for (const p of posts) p.data.categorySlugs.forEach((s) => cats.add(s));
  for (const c of cats) urls.push(`${SITE_URL}/cat/${c}/`);

  return new Response(urls.join('\n') + '\n', {
    headers: { 'Content-Type': 'text/plain; charset=utf-8' },
  });
};
