import { fieldsToJSONSchema } from '../../utilities/configToJSONSchema.js';
export function generateJobsJSONSchemas(config, jobsConfig, interfaceNameDefinitions, /**
   * Used for relationship fields, to determine whether to use a string or number type for the ID.
   * While there is a default ID field type set by the db adapter, they can differ on a collection-level
   * if they have custom ID fields.
   */ collectionIDFieldTypes) {
    const properties = {
        tasks: {},
        workflows: {}
    };
    const definitions = new Map();
    if (jobsConfig?.tasks?.length) {
        for (const task of jobsConfig.tasks){
            const fullTaskJsonSchema = {
                type: 'object',
                additionalProperties: false,
                properties: {
                    input: {},
                    output: {}
                },
                required: []
            };
            if (task?.inputSchema?.length) {
                const inputJsonSchema = fieldsToJSONSchema(collectionIDFieldTypes, task.inputSchema, interfaceNameDefinitions, config);
                const fullInputJsonSchema = {
                    type: 'object',
                    additionalProperties: false,
                    properties: inputJsonSchema.properties,
                    required: inputJsonSchema.required
                };
                fullTaskJsonSchema.properties.input = fullInputJsonSchema;
                fullTaskJsonSchema.required.push('input');
            }
            if (task?.outputSchema?.length) {
                const outputJsonSchema = fieldsToJSONSchema(collectionIDFieldTypes, task.outputSchema, interfaceNameDefinitions, config);
                const fullOutputJsonSchema = {
                    type: 'object',
                    additionalProperties: false,
                    properties: outputJsonSchema.properties,
                    required: outputJsonSchema.required
                };
                fullTaskJsonSchema.properties.output = fullOutputJsonSchema;
                fullTaskJsonSchema.required.push('output');
            }
            const normalizedTaskSlug = task.slug[0].toUpperCase() + task.slug.slice(1);
            definitions.set(task.interfaceName ?? `Task${normalizedTaskSlug}`, fullTaskJsonSchema);
        }
        // Now add properties.tasks definition that references the types in definitions keyed by task slug:
        properties.tasks = {
            type: 'object',
            additionalProperties: false,
            properties: {
                ...Object.fromEntries(jobsConfig.tasks.map((task)=>{
                    const normalizedTaskSlug = task.slug[0].toUpperCase() + task.slug.slice(1);
                    const toReturn = {
                        $ref: task.interfaceName ? `#/definitions/${task.interfaceName}` : `#/definitions/Task${normalizedTaskSlug}`
                    };
                    return [
                        task.slug,
                        toReturn
                    ];
                })),
                inline: {
                    type: 'object',
                    additionalProperties: false,
                    properties: {
                        input: {},
                        output: {}
                    },
                    required: [
                        'input',
                        'output'
                    ]
                }
            },
            required: [
                ...jobsConfig.tasks.map((task)=>task.slug),
                'inline'
            ]
        };
    }
    if (jobsConfig?.workflows?.length) {
        for (const workflow of jobsConfig.workflows){
            const fullWorkflowJsonSchema = {
                type: 'object',
                additionalProperties: false,
                properties: {
                    input: {}
                },
                required: []
            };
            if (workflow?.inputSchema?.length) {
                const inputJsonSchema = fieldsToJSONSchema(collectionIDFieldTypes, workflow.inputSchema, interfaceNameDefinitions, config);
                const fullInputJsonSchema = {
                    type: 'object',
                    additionalProperties: false,
                    properties: inputJsonSchema.properties,
                    required: inputJsonSchema.required
                };
                fullWorkflowJsonSchema.properties.input = fullInputJsonSchema;
                fullWorkflowJsonSchema.required.push('input');
            }
            const normalizedWorkflowSlug = workflow.slug[0].toUpperCase() + workflow.slug.slice(1);
            definitions.set(workflow.interfaceName ?? `Workflow${normalizedWorkflowSlug}`, fullWorkflowJsonSchema);
            properties.workflows = {
                type: 'object',
                additionalProperties: false,
                properties: Object.fromEntries(jobsConfig.workflows.map((workflow)=>{
                    const normalizedWorkflowSlug = workflow.slug[0].toUpperCase() + workflow.slug.slice(1);
                    const toReturn = {
                        $ref: workflow.interfaceName ? `#/definitions/${workflow.interfaceName}` : `#/definitions/Workflow${normalizedWorkflowSlug}`
                    };
                    return [
                        workflow.slug,
                        toReturn
                    ];
                })),
                required: jobsConfig.workflows.map((workflow)=>workflow.slug)
            };
        }
    }
    return {
        definitions,
        properties
    };
}

//# sourceMappingURL=generateJobsJSONSchemas.js.map