import { getLogger as getLogger$1 } from "../../../../logger.js";
import { ArtifactCreateSchema, ArtifactReferenceSchema } from "../../artifacts/artifact-component-schema.js";
import { z } from "@hono/zod-openapi";
import { normalizeDataComponentSchema } from "@inkeep/agents-core";

//#region src/domains/run/agents/generation/schema-builder.ts
const logger = getLogger$1("Agent");
function buildDataComponentsSchema(ctx) {
	const componentSchemas = [];
	ctx.config.dataComponents?.forEach((dc) => {
		const normalizedProps = normalizeDataComponentSchema(dc.props);
		const propsSchema = z.fromJSONSchema(normalizedProps);
		componentSchemas.push(z.object({
			id: z.string(),
			name: z.literal(dc.name),
			props: propsSchema
		}));
	});
	if (ctx.artifactComponents.length > 0) {
		const artifactCreateSchemas = ArtifactCreateSchema.getSchemas(ctx.artifactComponents);
		componentSchemas.push(...artifactCreateSchemas);
		componentSchemas.push(ArtifactReferenceSchema.getSchema());
	}
	let dataComponentsSchema;
	if (componentSchemas.length === 1) {
		dataComponentsSchema = componentSchemas[0];
		logger.info({ agentId: ctx.config.id }, "Using single schema (no union needed)");
	} else {
		dataComponentsSchema = z.union(componentSchemas);
		logger.info({ agentId: ctx.config.id }, "Created union schema");
	}
	return dataComponentsSchema;
}

//#endregion
export { buildDataComponentsSchema };