import { z } from 'zod';
function unwrap(schema) {
    let current = schema;
    let optional = false;
    let nullable = false;
    for (;;) {
        if (current instanceof z.ZodOptional) {
            optional = true;
            current = current._def.innerType;
            continue;
        }
        if (current instanceof z.ZodDefault) {
            optional = true;
            current = current._def.innerType;
            continue;
        }
        if (current instanceof z.ZodNullable) {
            nullable = true;
            current = current._def.innerType;
            continue;
        }
        if (current instanceof z.ZodPipe) {
            // Zod v4 represents transforms/pipes as a `ZodPipe` with `in`/`out` schemas.
            // For input contracts, we want to reflect the *input* schema shape.
            current = current._def.in;
            continue;
        }
        break;
    }
    return { schema: current, optional, nullable };
}
function mergeNullable(base, nullable) {
    if (!nullable)
        return base;
    // JSON Schema: represent nullable with a union for broad compatibility.
    return { anyOf: [base, { type: 'null' }] };
}
function schemaToJson(schema) {
    const { schema: core, nullable } = unwrap(schema);
    const out = (() => {
        if (core instanceof z.ZodObject) {
            const shape = core.shape ?? core._def?.shape ?? {};
            const properties = {};
            const required = [];
            for (const [key, value] of Object.entries(shape ?? {})) {
                const unwrapped = unwrap(value);
                properties[key] = schemaToJson(unwrapped.schema);
                if (!unwrapped.optional)
                    required.push(key);
            }
            const catchall = core._def?.catchall;
            const passthrough = catchall instanceof z.ZodUnknown;
            return {
                type: 'object',
                properties,
                ...(required.length > 0 ? { required } : {}),
                ...(passthrough ? { additionalProperties: true } : {}),
            };
        }
        if (core instanceof z.ZodArray) {
            const inner = core._def?.element;
            return { type: 'array', items: inner ? schemaToJson(inner) : {} };
        }
        if (core instanceof z.ZodString)
            return { type: 'string' };
        if (core instanceof z.ZodNumber)
            return { type: 'number' };
        if (core instanceof z.ZodBoolean)
            return { type: 'boolean' };
        if (core instanceof z.ZodLiteral) {
            const def = core._def;
            const value = def?.value ??
                (Array.isArray(def?.values) ? def.values[0] : undefined) ??
                (def?.values instanceof Set ? Array.from(def.values)[0] : undefined);
            if (value === null)
                return { type: 'null' };
            if (typeof value === 'string')
                return { type: 'string', enum: [value] };
            if (typeof value === 'number')
                return { type: 'number', enum: [value] };
            if (typeof value === 'boolean')
                return { type: 'boolean', enum: [value] };
            // Fallback: represent unknown literal as an unconstrained string.
            return { type: 'string' };
        }
        if (core instanceof z.ZodEnum) {
            const entries = core._def?.entries;
            const values = Object.values(entries ?? {}).filter((v) => typeof v === 'string');
            return { type: 'string', ...(values.length > 0 ? { enum: values } : {}) };
        }
        if (core instanceof z.ZodUnion) {
            const options = core._def?.options ?? [];
            return { oneOf: Array.isArray(options) ? options.map((s) => schemaToJson(s)) : [] };
        }
        if (core instanceof z.ZodDiscriminatedUnion) {
            const options = core._def?.options ?? [];
            return { oneOf: Array.isArray(options) ? options.map((s) => schemaToJson(s)) : [] };
        }
        // Fallback: accept any object.
        return { type: 'object', additionalProperties: true };
    })();
    return mergeNullable(out, nullable);
}
export function zodSchemaToJsonSchemaObject(schema) {
    return schemaToJson(schema);
}
//# sourceMappingURL=actionInputJsonSchema.js.map