import { z } from 'zod';
function jsonSchemaTypeToFieldType(schema) {
    if (!schema || typeof schema !== 'object')
        return 'any';
    if (Array.isArray(schema.anyOf) && schema.anyOf.length > 0) {
        const mapped = schema.anyOf.map((part) => jsonSchemaTypeToFieldType(part));
        return [...new Set(mapped)].join(' | ');
    }
    if (Array.isArray(schema.oneOf) && schema.oneOf.length > 0) {
        const mapped = schema.oneOf.map((part) => jsonSchemaTypeToFieldType(part));
        return [...new Set(mapped)].join(' | ');
    }
    if (Array.isArray(schema.type)) {
        const mapped = schema.type.map((t) => (t === 'integer' ? 'number' : t));
        return [...new Set(mapped)].join(' | ');
    }
    if (schema.type === 'integer')
        return 'number';
    if (schema.type === 'array') {
        if (schema.items) {
            const itemType = jsonSchemaTypeToFieldType(schema.items);
            return itemType === 'any' ? 'array' : `${itemType}[]`;
        }
        return 'array';
    }
    if (typeof schema.type === 'string')
        return schema.type;
    return 'any';
}
function fieldName(prefix, key) {
    return prefix ? `${prefix}.${key}` : key;
}
function extractFields(jsonSchema, prefix, requiredSet) {
    const properties = jsonSchema?.properties;
    if (!properties || typeof properties !== 'object')
        return [];
    const fields = [];
    for (const [key, value] of Object.entries(properties)) {
        const prop = value;
        const name = fieldName(prefix, key);
        fields.push({
            name,
            type: jsonSchemaTypeToFieldType(prop),
            description: typeof prop?.description === 'string' ? prop.description : '',
            required: requiredSet.has(key),
        });
        // Note: nullable objects (anyOf: [object, null]) are not recursed into.
        // Use a non-nullable sub-schema or manually list sub-fields if expansion is needed.
        if (prop.type === 'object' && prop.properties) {
            const nestedRequired = new Set(Array.isArray(prop.required) ? prop.required : []);
            fields.push(...extractFields(prop, name, nestedRequired));
        }
        if (prop.type === 'array' && prop.items?.type === 'object' && prop.items?.properties) {
            const itemRequired = new Set(Array.isArray(prop.items.required) ? prop.items.required : []);
            fields.push(...extractFields(prop.items, `${name}[]`, itemRequired));
        }
    }
    return fields;
}
function fieldsFromSchema(schema, prefix) {
    const jsonSchema = z.toJSONSchema(schema, { unrepresentable: 'any' });
    const required = new Set(Array.isArray(jsonSchema?.required) ? jsonSchema.required : []);
    return extractFields(jsonSchema, prefix, required);
}
function resolveFields(definition) {
    if (!definition)
        return undefined;
    if (Array.isArray(definition))
        return definition;
    let fields = fieldsFromSchema(definition.schema, definition.prefix);
    if (definition.pick) {
        const pickSet = new Set(definition.pick);
        fields = fields.filter((field) => pickSet.has(field.name));
    }
    if (definition.omit) {
        const omitSet = new Set(definition.omit);
        fields = fields.filter((field) => !omitSet.has(field.name));
    }
    if (definition.overrides) {
        fields = fields.map((field) => {
            const override = definition.overrides?.[field.name];
            return override ? { ...field, ...override } : field;
        });
    }
    if (definition.stripRequired) {
        fields = fields.map(({ required, ...rest }) => rest);
    }
    return fields;
}
export { fieldsFromSchema, resolveFields };
//# sourceMappingURL=api-reference.js.map