globalThis.process ??= {};
globalThis.process.env ??= {};
import { r as __exportAll } from "./rolldown-runtime_-UtK6b3T.mjs";
import { d as mapContentError, u as jsonOk } from "./crypto_CY2zBtG3.mjs";
import { A as resolveContentTypes, O as resolveContentStatuses, f as generateContentSchema, r as canonicalContentMigrationHash } from "./dist_YU938xCs.mjs";
import { n as getFullDb } from "./db_BQ9NAt8x.mjs";
import { $ as createFieldTypeRegistry, b as generateFieldsHeadlessSchema } from "./dist_BBLvfyU8.mjs";
import { env } from "cloudflare:workers";
//#region src/lib/content-model-headless.ts
function rowsOf(value) {
	if (Array.isArray(value)) return value;
	const rows = value?.rows;
	return Array.isArray(rows) ? rows : [];
}
function contentDefinitionReader(db) {
	return Object.freeze({ execute: async (query) => rowsOf(await db.execute(query)) });
}
function contentSchemaValue(value, path = "schema") {
	if (value === null || typeof value === "string" || typeof value === "boolean") return value;
	if (typeof value === "number") {
		if (!Number.isFinite(value)) throw new TypeError(`${path} contains a non-finite number`);
		return value;
	}
	if (Array.isArray(value)) return Object.freeze(value.map((item, index) => contentSchemaValue(item, `${path}.${index}`)));
	if (typeof value === "object") {
		const result = {};
		for (const key of Object.keys(value).sort()) {
			const child = value[key];
			if (child !== void 0) result[key] = contentSchemaValue(child, `${path}.${key}`);
		}
		return Object.freeze(result);
	}
	throw new TypeError(`${path} contains a non-serializable value`);
}
function valueMatches(operator, ruleValue, actual) {
	if (operator === "eq") return ruleValue === actual;
	if (operator === "neq") return ruleValue !== actual;
	if (operator === "in") return Array.isArray(ruleValue) && ruleValue.includes(actual);
	if (operator === "notIn") return !Array.isArray(ruleValue) || !ruleValue.includes(actual);
	if (operator === "contains") return typeof ruleValue === "string" && actual.includes(ruleValue);
	return true;
}
function groupMayApplyToType(group, typeKey) {
	return group.location.some((andGroup) => andGroup.every((rule) => {
		if (rule.parameter === "entityType") return valueMatches(rule.operator, rule.value, "content");
		if (rule.parameter === "subType") return valueMatches(rule.operator, rule.value, typeKey);
		return true;
	}));
}
function fieldsSchemaForType(fields, typeKey) {
	const groups = fields.groups.filter((group) => groupMayApplyToType(group, typeKey));
	const groupKeys = new Set(groups.map((group) => group.key));
	const blocks = fields.blocks.filter((block) => block.fieldGroupKeys.some((key) => groupKeys.has(key)));
	return contentSchemaValue({
		version: fields.version,
		groups,
		blocks
	}, `fields.${typeKey}`);
}
async function taxonomySchemas(keys) {
	const schema = Object.freeze({
		type: "object",
		properties: Object.freeze({
			id: Object.freeze({
				type: "string",
				format: "uuid"
			}),
			taxonomy: Object.freeze({ type: "string" }),
			slug: Object.freeze({ type: "string" }),
			name: Object.freeze({ type: "string" }),
			parentId: Object.freeze({
				type: ["string", "null"],
				format: "uuid"
			}),
			depth: Object.freeze({
				type: "integer",
				minimum: 0
			})
		}),
		required: Object.freeze([
			"id",
			"taxonomy",
			"slug",
			"name",
			"depth"
		])
	});
	return Object.freeze(await Promise.all([...new Set(keys)].sort().map(async (key) => Object.freeze({
		key,
		version: await canonicalContentMigrationHash({
			version: 1,
			key,
			schema
		}),
		schema
	}))));
}
async function generateContentModelHeadlessSchema(db, sources = {}) {
	const definitions = contentDefinitionReader(db);
	const [types, statuses, fields] = await Promise.all([
		resolveContentTypes(definitions, { codeTypes: sources.codeTypes }),
		resolveContentStatuses(definitions, { codeStatuses: sources.codeStatuses }),
		generateFieldsHeadlessSchema(db, {
			registry: sources.fieldsRegistry ?? createFieldTypeRegistry(),
			...sources.fieldsCodeRegistry ? { codeRegistry: sources.fieldsCodeRegistry } : {}
		})
	]);
	const taxonomies = await taxonomySchemas(types.flatMap((type) => type.taxonomies));
	const content = await generateContentSchema({
		types,
		statuses,
		taxonomies,
		fields: Object.freeze({ schemaForType: async (typeKey) => fieldsSchemaForType(fields, typeKey) })
	});
	const version = await canonicalContentMigrationHash({
		content: content.version,
		fields: fields.version
	});
	return Object.freeze({
		version,
		content,
		fields
	});
}
//#endregion
//#region src/pages/api/headless/schema.ts
var schema_exports = /* @__PURE__ */ __exportAll({
	GET: () => GET,
	prerender: () => false
});
var GET = async () => {
	try {
		const schema = await generateContentModelHeadlessSchema(getFullDb(env).db);
		return jsonOk(schema);
	} catch (error) {
		return mapContentError(error);
	}
};
//#endregion
//#region \0virtual:astro:page:src/pages/api/headless/schema@_@ts
var page = () => schema_exports;
//#endregion
export { page };
