"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateStep = void 0;
const zod_1 = require("zod");
const jsonSchema = zod_1.z.object({
    type: zod_1.z.string(),
    properties: zod_1.z.record(zod_1.z.any()),
    required: zod_1.z.array(zod_1.z.string()).optional(),
    additionalProperties: zod_1.z.boolean().optional(),
    description: zod_1.z.string().optional(),
    title: zod_1.z.string().optional(),
    definitions: zod_1.z.record(zod_1.z.any()).optional(),
    $ref: zod_1.z.string().optional(),
    items: zod_1.z.any().optional(),
    enum: zod_1.z.array(zod_1.z.any()).optional(),
    allOf: zod_1.z.array(zod_1.z.any()).optional(),
    anyOf: zod_1.z.array(zod_1.z.any()).optional(),
    oneOf: zod_1.z.array(zod_1.z.any()).optional(),
    not: zod_1.z.any().optional(),
    format: zod_1.z.string().optional(),
    default: zod_1.z.any().optional(),
    examples: zod_1.z.array(zod_1.z.any()).optional(),
    multipleOf: zod_1.z.number().optional(),
    maximum: zod_1.z.number().optional(),
    exclusiveMaximum: zod_1.z.union([zod_1.z.boolean(), zod_1.z.number()]).optional(),
    minimum: zod_1.z.number().optional(),
    exclusiveMinimum: zod_1.z.union([zod_1.z.boolean(), zod_1.z.number()]).optional(),
    maxLength: zod_1.z.number().optional(),
    minLength: zod_1.z.number().optional(),
    pattern: zod_1.z.string().optional(),
    maxItems: zod_1.z.number().optional(),
    minItems: zod_1.z.number().optional(),
    uniqueItems: zod_1.z.boolean().optional(),
    maxProperties: zod_1.z.number().optional(),
    minProperties: zod_1.z.number().optional(),
    const: zod_1.z.any().optional(),
});
const emits = zod_1.z.array(zod_1.z.union([
    zod_1.z.string(),
    zod_1.z
        .object({
        topic: zod_1.z.string(),
        label: zod_1.z.string().optional(),
        conditional: zod_1.z.boolean().optional(),
    })
        .strict(),
]));
const noopSchema = zod_1.z
    .object({
    type: zod_1.z.literal('noop'),
    name: zod_1.z.string(),
    description: zod_1.z.string().optional(),
    virtualEmits: emits,
    virtualSubscribes: zod_1.z.array(zod_1.z.string()),
    flows: zod_1.z.array(zod_1.z.string()).optional(),
})
    .strict();
const eventSchema = zod_1.z
    .object({
    type: zod_1.z.literal('event'),
    name: zod_1.z.string(),
    description: zod_1.z.string().optional(),
    subscribes: zod_1.z.array(zod_1.z.string()),
    emits: emits,
    virtualEmits: emits.optional(),
    input: zod_1.z.union([jsonSchema, zod_1.z.object({}), zod_1.z.null()]).optional(),
    flows: zod_1.z.array(zod_1.z.string()).optional(),
    includeFiles: zod_1.z.array(zod_1.z.string()).optional(),
})
    .strict();
const apiSchema = zod_1.z
    .object({
    type: zod_1.z.literal('api'),
    name: zod_1.z.string(),
    description: zod_1.z.string().optional(),
    path: zod_1.z.string(),
    method: zod_1.z.string(),
    emits: emits,
    virtualEmits: emits.optional(),
    virtualSubscribes: zod_1.z.array(zod_1.z.string()).optional(),
    flows: zod_1.z.array(zod_1.z.string()).optional(),
    includeFiles: zod_1.z.array(zod_1.z.string()).optional(),
    middleware: zod_1.z.array(zod_1.z.any()).optional(),
    queryParams: zod_1.z.array(zod_1.z.object({ name: zod_1.z.string(), description: zod_1.z.string().optional() })).optional(),
    bodySchema: zod_1.z.union([jsonSchema, zod_1.z.object({}), zod_1.z.null()]).optional(),
    responseSchema: zod_1.z.record(zod_1.z.string(), jsonSchema).optional(),
})
    .strict();
const cronSchema = zod_1.z
    .object({
    type: zod_1.z.literal('cron'),
    name: zod_1.z.string(),
    description: zod_1.z.string().optional(),
    cron: zod_1.z.string(),
    virtualEmits: emits.optional(),
    emits: emits,
    flows: zod_1.z.array(zod_1.z.string()).optional(),
    includeFiles: zod_1.z.array(zod_1.z.string()).optional(),
})
    .strict();
const validateStep = (step) => {
    try {
        if (step.config.type === 'noop') {
            noopSchema.parse(step.config);
        }
        else if (step.config.type === 'event') {
            eventSchema.parse(step.config);
        }
        else if (step.config.type === 'api') {
            apiSchema.parse(step.config);
        }
        else if (step.config.type === 'cron') {
            cronSchema.parse(step.config);
        }
        else {
            return {
                success: false,
                error: 'Invalid step type',
            };
        }
        return { success: true };
    }
    catch (error) {
        if (error instanceof zod_1.z.ZodError) {
            return {
                success: false,
                error: error.errors.map((err) => err.message).join(', '),
                errors: error.errors.map((err) => ({ path: err.path.join('.'), message: err.message })),
            };
        }
        // Handle unexpected errors
        return {
            success: false,
            error: 'Unexpected validation error occurred',
        };
    }
};
exports.validateStep = validateStep;
