"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FieldsValidationSchema = exports.fields = void 0;
const zod_1 = require("zod");
const extensions_core_1 = require("@awell-health/extensions-core");
const lodash_1 = require("lodash");
exports.fields = {
    endpoint: {
        id: 'endpoint',
        label: 'Endpoint',
        description: '',
        type: extensions_core_1.FieldType.STRING,
        required: true,
    },
    headers: {
        id: 'headers',
        label: 'Headers',
        description: '',
        type: extensions_core_1.FieldType.JSON,
        required: false,
    },
    jsonPayload: {
        id: 'jsonPayload',
        label: 'JSON payload',
        description: '',
        type: extensions_core_1.FieldType.JSON,
        required: true,
    },
    additionalPayload: {
        id: 'additionalPayload',
        label: 'Additional payload',
        description: 'The JSON payload and additional payload will be merged into a single payload',
        type: extensions_core_1.FieldType.JSON,
        required: false,
    },
};
exports.FieldsValidationSchema = zod_1.z.object({
    endpoint: zod_1.z.string().min(1),
    headers: zod_1.z
        .string()
        .trim()
        .optional()
        .transform((str, ctx) => {
        if ((0, lodash_1.isNil)(str) || (0, lodash_1.isEmpty)(str)) {
            return {};
        }
        try {
            const parsedJson = JSON.parse(str);
            if ((0, lodash_1.isEmpty)(parsedJson)) {
                return {};
            }
            if (typeof parsedJson !== 'object' ||
                Array.isArray(parsedJson) ||
                Object.values(parsedJson).some((value) => typeof value !== 'string')) {
                ctx.addIssue({
                    code: 'custom',
                    message: 'Headers must be a key-value object where each value is a string',
                });
                return zod_1.z.NEVER;
            }
            return parsedJson;
        }
        catch (e) {
            ctx.addIssue({ code: 'custom', message: 'Invalid JSON' });
            return zod_1.z.NEVER;
        }
    }),
    jsonPayload: zod_1.z
        .string()
        .trim()
        .transform((str, ctx) => {
        if ((0, lodash_1.isNil)(str) || (0, lodash_1.isEmpty)(str)) {
            return {};
        }
        try {
            const parsedJson = JSON.parse(str);
            if ((0, lodash_1.isEmpty)(parsedJson)) {
                return {};
            }
            return parsedJson;
        }
        catch (e) {
            ctx.addIssue({ code: 'custom', message: 'Invalid JSON' });
            return zod_1.z.NEVER;
        }
    }),
    additionalPayload: zod_1.z
        .string()
        .trim()
        .optional()
        .transform((str, ctx) => {
        if ((0, lodash_1.isNil)(str) || (0, lodash_1.isEmpty)(str)) {
            return {};
        }
        try {
            const parsedJson = JSON.parse(str);
            if ((0, lodash_1.isEmpty)(parsedJson)) {
                return {};
            }
            return parsedJson;
        }
        catch (e) {
            ctx.addIssue({ code: 'custom', message: 'Invalid JSON' });
            return zod_1.z.NEVER;
        }
    }),
});
//# sourceMappingURL=fields.js.map