import "../../../base64-CmWvODNW.mjs";
import "../../../types-CuW0zWDA.mjs";
import { n as apiSuccess, r as handleError, t as apiError } from "../../../error-CkAseSqZ.mjs";

//#region src/astro/routes/api/typegen.ts
const prerender = false;
/**
* Safely list collections, returning empty array if tables don't exist yet
*/
async function safeListCollections(registry) {
	try {
		return await registry.listCollections();
	} catch (error) {
		if (error instanceof Error && error.message.includes("no such table")) return [];
		throw error;
	}
}
/**
* Generate types content and metadata from the current schema.
*/
async function generateTypes(registry) {
	const { generateTypesFile, generateSchemaHash } = await import("../../../zod-generator-CacrR-i8.mjs").then((n) => n.i);
	const collections = await safeListCollections(registry);
	const collectionsWithFields = await Promise.all(collections.map(async (c) => {
		const fields = await registry.listFields(c.id);
		return {
			...c,
			fields
		};
	}));
	return {
		types: generateTypesFile(collectionsWithFields),
		hash: await generateSchemaHash(collectionsWithFields),
		collections: collections.length
	};
}
/**
* GET - Return types as plain text (for preview/debugging)
*/
const GET = async ({ locals }) => {
	if (!import.meta.env.DEV) return apiError("FORBIDDEN", "Typegen is only available in development", 403);
	const { emdash } = locals;
	if (!emdash?.db) return apiError("NOT_CONFIGURED", "EmDash not configured", 500);
	try {
		const { SchemaRegistry } = await import("../../../registry-B65mUtPa.mjs").then((n) => n.r);
		const { types } = await generateTypes(new SchemaRegistry(emdash.db));
		return new Response(types, {
			status: 200,
			headers: {
				"Content-Type": "text/typescript",
				"Cache-Control": "private, no-store"
			}
		});
	} catch (error) {
		return handleError(error, "Typegen failed", "TYPEGEN_ERROR");
	}
};
/**
* POST - Generate types and return as JSON
*
* The caller writes the file to disk. Response shape:
* { types: string, hash: string, collections: number }
*/
const POST = async ({ locals }) => {
	if (!import.meta.env.DEV) return apiError("FORBIDDEN", "Typegen is only available in development", 403);
	const { emdash } = locals;
	if (!emdash?.db) return apiError("NOT_CONFIGURED", "EmDash not configured", 500);
	try {
		const { SchemaRegistry } = await import("../../../registry-B65mUtPa.mjs").then((n) => n.r);
		return apiSuccess(await generateTypes(new SchemaRegistry(emdash.db)));
	} catch (error) {
		return handleError(error, "Typegen failed", "TYPEGEN_ERROR");
	}
};

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