import { createLogger } from "./utils.js";
import path from "node:path";
import { writeFileSync } from "node:fs";
import { create } from "xmlbuilder2";
//#region src/build-sitemap.ts
function buildSitemapJson(pages, host) {
	const slash = checkSlash(host);
	return { urls: pages.filter((page) => {
		return page.sitemap?.exclude !== true;
	}).map((page) => ({
		loc: `${host}${slash}${page.path.replace(/^\/+/g, "")}`,
		lastmod: page.sitemap?.lastmod ? new Date(page.sitemap.lastmod).toISOString().split("T")[0] : (/* @__PURE__ */ new Date()).toISOString().split("T")[0],
		priority: page.sitemap?.priority,
		changefreq: page.sitemap?.changefreq,
		alternateRefs: page.sitemap?.alternateRefs,
		images: page.sitemap?.images,
		news: page.sitemap?.news
	})) };
}
function jsonToXml(sitemapData) {
	const sitemap = createXml("urlset");
	for (const item of sitemapData.urls) {
		const page = sitemap.ele("url");
		page.ele("loc").txt(item.loc);
		page.ele("lastmod").txt(item.lastmod);
		if (item.priority !== void 0) page.ele("priority").txt(item.priority.toString());
		if (item.changefreq) page.ele("changefreq").txt(item.changefreq);
		if (item.alternateRefs?.length) for (const ref of item.alternateRefs) {
			const alternateRef = page.ele("xhtml:link");
			alternateRef.att("rel", "alternate");
			alternateRef.att("href", ref.href);
			if (ref.hreflang) alternateRef.att("hreflang", ref.hreflang);
		}
		if (item.images?.length) for (const image of item.images) {
			const imageElement = page.ele("image:image");
			imageElement.ele("image:loc").txt(image.loc);
			if (image.title) imageElement.ele("image:title").txt(image.title);
			if (image.caption) imageElement.ele("image:caption").txt(image.caption);
		}
		if (item.news) {
			const newsElement = page.ele("news:news");
			const publication = newsElement.ele("news:publication");
			publication.ele("news:name").txt(item.news.publication.name);
			publication.ele("news:language").txt(item.news.publication.language);
			newsElement.ele("news:publication_date").txt(new Date(item.news.publicationDate).toISOString().split("T")[0]);
			newsElement.ele("news:title").txt(item.news.title);
		}
	}
	return sitemap.end({ prettyPrint: true });
}
function buildSitemap({ startConfig, publicDir }) {
	const logger = createLogger("sitemap");
	let sitemapOptions = startConfig.sitemap;
	if (!sitemapOptions && startConfig.pages.length) sitemapOptions = {
		enabled: true,
		outputPath: "sitemap.xml"
	};
	if (!sitemapOptions?.enabled) throw new Error("Sitemap is not enabled");
	const { host, outputPath } = sitemapOptions;
	if (!host) {
		if (!startConfig.sitemap) {
			logger.info("Hint: Pages found, but no sitemap host has been set. To enable sitemap generation, set the `sitemap.host` option.");
			return;
		}
		throw new Error("Sitemap host is not set and required to build the sitemap.");
	}
	if (!outputPath) throw new Error("Sitemap output path is not set");
	const { pages } = startConfig;
	if (!pages.length) {
		logger.info("No pages were found to build the sitemap. Skipping...");
		return;
	}
	logger.info("Building Sitemap...");
	const sitemapData = buildSitemapJson(pages, host);
	const xmlOutputPath = path.join(publicDir, outputPath);
	const pagesOutputPath = path.join(publicDir, "pages.json");
	try {
		logger.info(`Writing sitemap XML at ${xmlOutputPath}`);
		writeFileSync(xmlOutputPath, jsonToXml(sitemapData));
		logger.info(`Writing pages data at ${pagesOutputPath}`);
		writeFileSync(pagesOutputPath, JSON.stringify({
			pages,
			host,
			lastBuilt: (/* @__PURE__ */ new Date()).toISOString()
		}, null, 2));
	} catch (e) {
		logger.error(`Unable to write sitemap files`, e);
	}
}
function createXml(elementName) {
	return create({
		version: "1.0",
		encoding: "UTF-8"
	}).ele(elementName, {
		xmlns: "https://www.sitemaps.org/schemas/sitemap/0.9",
		"xmlns:xhtml": "http://www.w3.org/1999/xhtml"
	}).com(`This file was automatically generated by TanStack Start.`);
}
function checkSlash(host) {
	return host.slice(-1) === "/" ? "" : "/";
}
//#endregion
export { buildSitemap };

//# sourceMappingURL=build-sitemap.js.map