//#region src/sitemap.ts
function escapeXml(value) {
	return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
}
function buildUrlEntry(loc, options = {}) {
	return {
		loc,
		...options.lastmod ? { lastmod: options.lastmod } : {},
		...options.hrefLangs?.length ? { hrefLangs: options.hrefLangs } : {}
	};
}
function renderHrefLangAlternates(links) {
	return links.map((link) => `    <xhtml:link rel="alternate" hreflang="${escapeXml(link.hreflang)}" href="${escapeXml(link.href)}"/>`).join("\n");
}
function renderUrlNode(entry) {
	const lines = [`  <url>`, `    <loc>${escapeXml(entry.loc)}</loc>`];
	if (entry.lastmod) lines.push(`    <lastmod>${escapeXml(entry.lastmod)}</lastmod>`);
	if (entry.hrefLangs?.length) lines.push(renderHrefLangAlternates(entry.hrefLangs));
	lines.push("  </url>");
	return lines.join("\n");
}
function renderUrlSet(urls) {
	return `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">\n${urls.map(renderUrlNode).join("\n")}\n</urlset>\n`;
}
function renderSitemapIndex(children) {
	return `<?xml version="1.0" encoding="UTF-8"?>\n<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${children.map((child) => {
		const lines = [`  <sitemap>`, `    <loc>${escapeXml(child.loc)}</loc>`];
		if (child.lastmod) lines.push(`    <lastmod>${escapeXml(child.lastmod)}</lastmod>`);
		lines.push("  </sitemap>");
		return lines.join("\n");
	}).join("\n")}\n</sitemapindex>\n`;
}
//#endregion
export { buildUrlEntry, renderSitemapIndex, renderUrlSet };
