globalThis.process ??= {};
globalThis.process.env ??= {};
import { r as __exportAll } from "./rolldown-runtime_-UtK6b3T.mjs";
import { d as renderTemplate, f as maybeRenderHead, i as renderComponent, m as addAttribute, w as createAstro } from "./server_DAChoow1.mjs";
import { t as createComponent } from "./compiler_CLrjY88v.mjs";
import { n as getFullDb } from "./db_BQ9NAt8x.mjs";
import { S as getEntityValues } from "./dist_BBLvfyU8.mjs";
import { a as $$DefaultPostBody, p as resolveScreen, t as resolveModTheme } from "./registry_CB_IcduS.mjs";
import { t as $$Base } from "./Base_AuO_PBeT.mjs";
import { n as getCommentsDb } from "./comments_Drdrk1x-.mjs";
import { r as loadPublicComments } from "./comments-render_BZgKznVO.mjs";
import { t as loadAppearanceSelection } from "./appearance-server_BGcx6aOu.mjs";
import { a as resolveContentModelRouteTemplate, i as resolveContentModelPublicRoute, t as CONTENT_PROOF_COOKIE } from "./content-model-routes_7Z0Wp5YY.mjs";
import { i as listLanguages } from "./dist_B46NNvzD.mjs";
import { n as resolveContentTranslations, t as coalesceFields } from "./content-i18n_CtIqsgpa.mjs";
import { t as getEntityTranslationStatus } from "./dist_Bl2ZZDeR.mjs";
import { i as getFieldsDb, t as DEFAULT_CONTENT_FIELD_GROUP } from "./fields_DI7yizwv.mjs";
import { env } from "cloudflare:workers";
//#region ../../packages/i18n/dist/index.js
var I18nConfigError = class extends Error {
	offending;
	constructor(message, offending) {
		super(message);
		this.offending = offending;
		this.name = "I18nConfigError";
	}
};
function createI18n(config) {
	if (!config.locales.includes(config.defaultLocale)) throw new I18nConfigError(`createI18n: defaultLocale "${config.defaultLocale}" not in locales [${config.locales.join(", ")}]`, {
		defaultLocale: config.defaultLocale,
		locales: config.locales
	});
	for (const locale of config.locales) if (!Object.hasOwn(config.messages, locale)) throw new I18nConfigError(`createI18n: messages missing entry for locale "${locale}"`, {
		missingLocale: locale,
		locales: config.locales
	});
	return Object.freeze({ ...config });
}
function resolveLocale(i18n, request) {
	const seg = new URL(request.url).pathname.split("/")[1];
	if (seg && i18n.locales.includes(seg)) return seg;
	const m = (request.headers.get("cookie") ?? "").match(/(?:^|;\s*)locale=([^;]+)/);
	if (m && i18n.locales.includes(m[1])) return m[1];
	const al = request.headers.get("accept-language") ?? "";
	for (const part of al.split(",")) {
		const code = part.trim().split(/[-;]/)[0];
		if (code && i18n.locales.includes(code)) return code;
	}
	return i18n.defaultLocale;
}
//#endregion
//#region src/lib/content-visibility.ts
function contentVisibleInLocale(authzVisible, translationStatus, isDefaultLocale) {
	if (isDefaultLocale) return authzVisible;
	if (translationStatus == null) return false;
	return authzVisible && translationStatus === "complete";
}
//#endregion
//#region src/pages/post/[slug].astro
var _slug__exports = /* @__PURE__ */ __exportAll({
	default: () => $$Slug,
	file: () => $$file,
	prerender: () => false,
	url: () => $$url
});
createAstro("https://astro.build");
var $$Slug = createComponent(async ($$result, $$props, $$slots) => {
	const Astro = $$result.createAstro($$props, $$slots);
	Astro.self = $$Slug;
	const slug = Astro.params.slug;
	const db = getCommentsDb(env);
	const fullDb = getFullDb(env).db;
	const fieldsDb = getFieldsDb(env);
	const routeContext = slug ? await resolveContentModelPublicRoute(env, Astro.url, Astro.cookies.get(CONTENT_PROOF_COOKIE)?.value) : null;
	const routeResolution = routeContext?.resolution.kind === "single" && routeContext.resolution.match.typeKey === "post" ? routeContext.resolution : null;
	if (routeContext && routeResolution) resolveContentModelRouteTemplate(routeContext);
	const lockedPost = routeResolution?.content.access === "passwordRequired" ? routeResolution.content.entry : null;
	let post = routeResolution?.content.access === "granted" ? routeResolution.content.entry : null;
	if (post) {
		const languages = await listLanguages(fullDb, { activeOnly: true });
		const localeCodes = languages.length > 0 ? languages.map((l) => l.code) : ["en"];
		const i18nConfig = createI18n({
			locales: localeCodes,
			defaultLocale: languages.find((l) => l.isDefault)?.code ?? "en",
			messages: Object.fromEntries(localeCodes.map((code) => [code, {}]))
		});
		const resolvedLocale = resolveLocale(i18nConfig, Astro.request);
		if (!(resolvedLocale === i18nConfig.defaultLocale)) {
			if (!contentVisibleInLocale(true, await getEntityTranslationStatus(fullDb, {
				entityType: "content",
				entityId: post.id,
				locale: resolvedLocale,
				fields: ["title", "body"]
			}), false)) post = null;
			else {
				const entityTranslations = (await resolveContentTranslations(fullDb, {
					entityType: "content",
					entityIds: [post.id],
					locale: resolvedLocale,
					fields: ["title", "body"]
				})).get(post.id) ?? {};
				if (!["title", "body"].every((f) => entityTranslations[f] !== void 0 && entityTranslations[f] !== "")) post = null;
				else post = coalesceFields(post, entityTranslations, ["title", "body"]);
			}
		}
	}
	let fieldValues = {};
	if (post) try {
		fieldValues = await getEntityValues(fieldsDb, {
			entityType: "content",
			entityId: post.id
		}, {
			entityType: "content",
			subType: post.type,
			codeGroups: [DEFAULT_CONTENT_FIELD_GROUP]
		});
	} catch {
		fieldValues = {};
	}
	const fields = DEFAULT_CONTENT_FIELD_GROUP.fields.map((f) => ({
		key: f.key,
		label: f.label,
		type: f.type,
		value: fieldValues[f.key]
	})).filter((x) => x.value !== void 0 && x.value !== null && x.value !== "");
	let commentTotal = 0;
	let comments = [];
	if (post) try {
		const loaded = await loadPublicComments(db, {
			type: "content_entry",
			id: post.id
		});
		commentTotal = loaded.total;
		comments = loaded.comments;
	} catch {
		commentTotal = 0;
		comments = [];
	}
	if (!post && !lockedPost) Astro.response.status = 404;
	const selection = await loadAppearanceSelection(env);
	let PostBody = $$DefaultPostBody;
	if (post) PostBody = resolveScreen(resolveModTheme(selection.themeId), "post", $$DefaultPostBody);
	return renderTemplate`${post ? renderTemplate`${renderComponent($$result, "Base", $$Base, {
		"title": post.title,
		"selection": selection
	}, { "default": ($$result) => renderTemplate`${renderComponent($$result, "PostBody", PostBody, {
		"post": post,
		"comments": comments,
		"commentTotal": commentTotal,
		"fields": fields
	})}` })}` : lockedPost ? renderTemplate`${renderComponent($$result, "Base", $$Base, {
		"title": lockedPost.title,
		"noindex": true,
		"selection": selection
	}, { "default": ($$result) => renderTemplate`${maybeRenderHead($$result)}<section class="section"><h1>${lockedPost.title}</h1><p>This content is password protected.</p><form method="post" action="/api/content/unlock"><input type="hidden" name="entryId"${addAttribute(lockedPost.id, "value")}><input type="hidden" name="returnTo"${addAttribute(`${Astro.url.pathname}${Astro.url.search}`, "value")}><label><span>Password</span><input name="password" type="password" required autocomplete="current-password"></label><button type="submit">Unlock content</button></form></section>` })}` : renderTemplate`${renderComponent($$result, "Base", $$Base, {
		"title": "Not found",
		"noindex": true,
		"selection": selection
	}, { "default": ($$result) => renderTemplate`<h1>Post not found</h1><p><a href="/">← All posts</a></p>` })}`}`;
}, "/home/user/runs/run-20260823T175424Z-2fpke4/repo/apps/mod-cms/src/pages/post/[slug].astro", void 0);
var $$file = "/home/user/runs/run-20260823T175424Z-2fpke4/repo/apps/mod-cms/src/pages/post/[slug].astro";
var $$url = "/post/[slug]";
//#endregion
//#region \0virtual:astro:page:src/pages/post/[slug]@_@astro
var page = () => _slug__exports;
//#endregion
export { page };
