import { n as getLogger } from "./logger.js";
import { z } from "@hono/zod-openapi";

//#region src/utils/schema-conversion.ts
const logger = getLogger("schema-conversion");
/**
* Converts JSON Schema to Zod schema at runtime.
*
* This is useful for dynamically creating Zod schemas from JSON Schema definitions,
* such as when using AI SDK's generateObject/streamObject with data component props.
*
* Note: Uses z.string() as fallback for unknown types to ensure Anthropic compatibility
* (Anthropic requires explicit types - z.any()/z.unknown() produce invalid JSON Schema).
*/
function jsonSchemaToZod(jsonSchema) {
	if (!jsonSchema || typeof jsonSchema !== "object") {
		logger.warn({ jsonSchema }, "Invalid JSON schema provided, using string fallback");
		return z.string();
	}
	const schemaType = jsonSchema.type;
	switch (schemaType) {
		case "object": {
			const properties = jsonSchema.properties;
			if (properties && typeof properties === "object") {
				const shape = {};
				for (const [key, prop] of Object.entries(properties)) shape[key] = jsonSchemaToZod(prop);
				return z.object(shape);
			}
			return z.record(z.string(), z.string());
		}
		case "array": {
			const items = jsonSchema.items;
			const itemSchema = items ? jsonSchemaToZod(items) : z.string();
			return z.array(itemSchema);
		}
		case "string": return z.string();
		case "number":
		case "integer": return z.number();
		case "boolean": return z.boolean();
		case "null": return z.null();
		default:
			logger.warn({
				unsupportedType: schemaType,
				schema: jsonSchema
			}, "Unsupported JSON schema type, using string fallback");
			return z.string();
	}
}
/**
* Utility function for converting Zod schemas to JSON Schema
* Uses Zod's built-in toJSONSchema method
*/
function convertZodToJsonSchema(zodSchema) {
	try {
		const jsonSchema = z.toJSONSchema(zodSchema);
		if (jsonSchema.$schema) delete jsonSchema.$schema;
		return jsonSchema;
	} catch (error) {
		logger.error({
			error: error instanceof Error ? error.message : "Unknown error",
			stack: error instanceof Error ? error.stack : void 0
		}, "Failed to convert Zod schema to JSON Schema");
		throw new Error("Failed to convert Zod schema to JSON Schema");
	}
}
/**
* Simple helper to mark a Zod schema field as a preview field
* Adds metadata to the schema definition without modifying Zod's core
*/
const preview = (schema) => {
	schema._def.inPreview = true;
	return schema;
};
/**
* Convert Zod schema to JSON Schema while preserving preview metadata
*/
function convertZodToJsonSchemaWithPreview(zodSchema) {
	const jsonSchema = convertZodToJsonSchema(zodSchema);
	if (zodSchema instanceof z.ZodObject && jsonSchema.properties) {
		const shape = zodSchema.shape;
		for (const [key, fieldSchema] of Object.entries(shape)) if (fieldSchema?._def?.inPreview === true) jsonSchema.properties[key].inPreview = true;
	}
	return jsonSchema;
}
/**
* Type guard to check if a value is a Zod schema
*/
function isZodSchema(value) {
	return value?._def?.type === "object";
}
/**
* Extract preview fields from either JSON Schema or Zod schema
*/
function extractPreviewFields(schema) {
	const previewFields = [];
	if (schema instanceof z.ZodObject) {
		const shape = schema.shape;
		for (const [key, fieldSchema] of Object.entries(shape)) if (fieldSchema?._def?.inPreview === true) previewFields.push(key);
		return previewFields;
	}
	if (schema?.type === "object" && schema.properties) {
		for (const [key, prop] of Object.entries(schema.properties)) if (prop.inPreview === true) previewFields.push(key);
	}
	return previewFields;
}

//#endregion
export { jsonSchemaToZod as a, isZodSchema as i, convertZodToJsonSchemaWithPreview as n, preview as o, extractPreviewFields as r, convertZodToJsonSchema as t };
//# sourceMappingURL=schema-conversion.js.map