import { notFound } from "next/navigation"
import { getSitemapDataById } from "@/utils/sitemap"
import type { NextRequest } from "next/server"
export const dynamic = "force-dynamic"
export async function GET(
_request: NextRequest,
context: { params: Promise<{ sitemapId: string }> }
) {
const params = await context.params
const sitemapId = params.sitemapId
console.log("[SITEMAP] Fetching sitemap by ID", sitemapId)
if (!sitemapId) {
return notFound()
}
const sitemapData = await getSitemapDataById(Number(sitemapId))
if (!sitemapData.length) {
return notFound()
}
const entries = sitemapData.map((entry) => {
const alternates = Object.entries(entry.alternates).map(
([lang, url]) =>
``
)
return `
${entry.url}
${entry.lastModified}
${entry.changeFrequency}
${entry.priority}
${alternates.join("")}
`
})
const sitemapXML = `\n${entries.join("")}\n`
return new Response(sitemapXML, {
headers: { "Content-Type": "application/xml" },
})
}