import "../../../../dialect-helpers-DRI5pyY3.mjs";
import "../../../../base64-CmWvODNW.mjs";
import "../../../../types-CuW0zWDA.mjs";
import "../../../../content-refresh-CA4uKwzH.mjs";
import "../../../../fts-manager-BbFaF_V8.mjs";
import { n as SchemaRegistry$1 } from "../../../../registry-B65mUtPa.mjs";
import { r as uniqueInterfaceNames, t as generateTypeScript } from "../../../../zod-generator-CacrR-i8.mjs";
import { i as requireDb, n as apiSuccess, r as handleError } from "../../../../error-CkAseSqZ.mjs";
import { n as requirePerm } from "../../../../authorize-Dz88F_Fw.mjs";
import { hashString } from "emdash";

//#region src/astro/routes/api/schema/index.ts
const prerender = false;
const GET = async ({ request, locals }) => {
	const { emdash, user } = locals;
	const dbErr = requireDb(emdash?.db);
	if (dbErr) return dbErr;
	const denied = requirePerm(user, "schema:read");
	if (denied) return denied;
	try {
		const registry = new SchemaRegistry$1(emdash.db);
		const collections = await registry.listCollections();
		const collectionsWithFields = await Promise.all(collections.map(async (c) => {
			const fields = await registry.listFields(c.id);
			return {
				...c,
				fields
			};
		}));
		if (new URL(request.url).searchParams.get("format") === "typescript") {
			const interfaceNames = uniqueInterfaceNames(collectionsWithFields);
			const types = collectionsWithFields.map((c) => generateTypeScript(c, interfaceNames.get(c.slug))).join("\n\n");
			return new Response(`// Generated by EmDash CLI
// Do not edit manually - run \`emdash types\` to regenerate

import type { PortableTextBlock } from "emdash";

` + types, {
				status: 200,
				headers: {
					"Content-Type": "text/typescript",
					"Cache-Control": "private, no-store"
				}
			});
		}
		const schemaExport = { collections: collectionsWithFields.map((c) => ({
			slug: c.slug,
			label: c.label,
			labelSingular: c.labelSingular,
			description: c.description,
			icon: c.icon,
			supports: c.supports,
			fields: c.fields.map((f) => ({
				slug: f.slug,
				label: f.label,
				type: f.type,
				required: f.required,
				unique: f.unique,
				defaultValue: f.defaultValue,
				validation: f.validation,
				widget: f.widget,
				options: f.options
			}))
		})) };
		const version = await hashString(JSON.stringify(schemaExport));
		const response = apiSuccess({
			...schemaExport,
			version
		});
		response.headers.set("X-Schema-Version", version);
		return response;
	} catch (error) {
		return handleError(error, "Schema export failed", "SCHEMA_EXPORT_ERROR");
	}
};

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