import "../../dialect-helpers-DRI5pyY3.mjs";
import "../../after-B1IIdH3Y.mjs";
import "../../object-cache-Bok5j2ae.mjs";
import "../../base64-CmWvODNW.mjs";
import "../../types-CuW0zWDA.mjs";
import "../../media-BrXIaMFN.mjs";
import "../../options-DTTML-Tx.mjs";
import "../../request-cache-KCNHp_RJ.mjs";
import "../../loader-CB6YHeul.mjs";
import { t as handleSitemapData } from "../../seo-89-53DDs.mjs";
import { a as getSiteSettingsWithDb } from "../../settings-zhmdKyJQ.mjs";
import { n as getPublicOrigin } from "../../public-url-Danv47cS.mjs";

//#region src/astro/routes/sitemap.xml.ts
const prerender = false;
const TRAILING_SLASH_RE = /\/$/;
const AMP_RE = /&/g;
const LT_RE = /</g;
const GT_RE = />/g;
const QUOT_RE = /"/g;
const APOS_RE = /'/g;
const GET = async ({ locals, url }) => {
	const { emdash } = locals;
	if (!emdash?.db) return new Response("<!-- EmDash not configured -->", {
		status: 500,
		headers: { "Content-Type": "application/xml" }
	});
	try {
		const siteUrl = ((await getSiteSettingsWithDb(emdash.db)).url || getPublicOrigin(url, emdash?.config)).replace(TRAILING_SLASH_RE, "");
		const result = await handleSitemapData(emdash.db);
		if (!result.success || !result.data) return new Response("<!-- Failed to generate sitemap -->", {
			status: 500,
			headers: { "Content-Type": "application/xml" }
		});
		const { collections } = result.data;
		const lines = ["<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">"];
		for (const col of collections) {
			const loc = `${siteUrl}/sitemap-${encodeURIComponent(col.collection)}.xml`;
			lines.push("  <sitemap>");
			lines.push(`    <loc>${escapeXml(loc)}</loc>`);
			lines.push(`    <lastmod>${escapeXml(col.lastmod)}</lastmod>`);
			lines.push("  </sitemap>");
		}
		lines.push("</sitemapindex>");
		return new Response(lines.join("\n"), {
			status: 200,
			headers: {
				"Content-Type": "application/xml; charset=utf-8",
				"Cache-Control": "public, max-age=3600"
			}
		});
	} catch {
		return new Response("<!-- Internal error generating sitemap -->", {
			status: 500,
			headers: { "Content-Type": "application/xml" }
		});
	}
};
/** Escape special XML characters in a string */
function escapeXml(str) {
	return str.replace(AMP_RE, "&amp;").replace(LT_RE, "&lt;").replace(GT_RE, "&gt;").replace(QUOT_RE, "&quot;").replace(APOS_RE, "&apos;");
}

//#endregion
export { GET, prerender };
//# sourceMappingURL=sitemap.xml.mjs.map