import type { MetadataRoute } from "next";
import { source } from "./source";

export const revalidate = false;

const baseUrl =
  process.env.NODE_ENV === "development" || !process.env.VERCEL_URL
    ? new URL("http://localhost:3000")
    : new URL(`https://${process.env.VERCEL_URL}`);

export default function sitemap(): MetadataRoute.Sitemap {
  const url = (path: string): string => new URL(path, baseUrl).toString();

  return [
    {
      url: url("/"),
      changeFrequency: "monthly",
      priority: 1,
    },
    ...source.getPages().map<MetadataRoute.Sitemap[number]>((page) => ({
      url: url(page.url),
      lastModified: (page.data as any).lastModified
        ? new Date((page.data as any).lastModified)
        : undefined,
      changeFrequency: "weekly",
      priority: 0.5,
    })),
  ];
}
