import { BestAgentError } from "@best-agent/foundation";
import { z } from "zod";
const TOOL_INPUT_SCHEMA_KIND = "best-agent.tool-input-schema";
export function toolInputSchema(schema, options = {}) {
    const jsonSchema = options.jsonSchema ?? z.toJSONSchema(schema);
    delete jsonSchema.$schema;
    return {
        jsonSchema,
        kind: TOOL_INPUT_SCHEMA_KIND,
        zodSchema: schema,
    };
}
/** Create a typed tool input schema whose output type binds directly to TInput.
 *  Use this when the Zod schema output IS the same type as TInput on the ToolDefinition. */
export function typedToolInputSchema(schema, options = {}) {
    const jsonSchema = options.jsonSchema ?? z.toJSONSchema(schema);
    delete jsonSchema.$schema;
    return {
        jsonSchema,
        kind: TOOL_INPUT_SCHEMA_KIND,
        zodSchema: schema,
    };
}
export function isZodToolInputSchema(inputSchema) {
    return (Boolean(inputSchema) &&
        typeof inputSchema === "object" &&
        inputSchema.kind === TOOL_INPUT_SCHEMA_KIND);
}
export function getToolInputJsonSchema(inputSchema) {
    return isZodToolInputSchema(inputSchema) ? inputSchema.jsonSchema : inputSchema;
}
export function parseZodToolInput(toolName, inputSchema, input) {
    const parsed = inputSchema.zodSchema.safeParse(input);
    if (parsed.success) {
        return parsed.data;
    }
    const issue = parsed.error.issues[0];
    const path = issue?.path.length ? ` input.${issue.path.join(".")}` : " input";
    throw new BestAgentError("TOOL_INPUT_INVALID", `${toolName}${path} does not match the expected schema.`);
}
/** Build a plain JSON Schema from a Zod schema, for use as a wire-format
 *  description on legacy tools where the schema shape differs from TInput.
 *  The legacyAdapter is responsible for full validation and normalization;
 *  the returned schema is only used to generate the LLM-facing JSON Schema. */
export function wireInputSchema(schema, options = {}) {
    const jsonSchema = options.jsonSchema ?? z.toJSONSchema(schema);
    delete jsonSchema.$schema;
    return jsonSchema;
}
//# sourceMappingURL=schema.js.map