"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FieldsValidationSchema = exports.fields = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const zod_1 = __importDefault(require("zod"));
const lodash_1 = require("lodash");
exports.fields = {
    emailId: {
        id: 'emailId',
        label: 'Email ID',
        description: 'The ID of the email (template)',
        type: extensions_core_1.FieldType.STRING,
        required: true,
    },
    from: {
        id: 'from',
        label: 'From',
        description: '',
        type: extensions_core_1.FieldType.STRING,
        required: true,
    },
    to: {
        id: 'to',
        label: 'To',
        description: '',
        type: extensions_core_1.FieldType.STRING,
        required: true,
    },
    contactProperties: {
        id: 'contactProperties',
        label: 'Contact properties',
        description: 'A JSON map of contact property values. Each property will be set on the contact record.',
        type: extensions_core_1.FieldType.JSON,
        required: false,
    },
    customProperties: {
        id: 'customProperties',
        label: 'Custom properties',
        description: 'A JSON map of key-value properties that will be rendered in the template.',
        type: extensions_core_1.FieldType.JSON,
        required: false,
    },
};
exports.FieldsValidationSchema = zod_1.default.object({
    emailId: zod_1.default.string().min(1),
    from: zod_1.default.string().min(1),
    to: zod_1.default.string().min(1),
    contactProperties: zod_1.default
        .string()
        .optional()
        .transform((str, ctx) => {
        if ((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: 'Contact properties must be a key-value map where each value is a string',
                });
                return zod_1.default.NEVER;
            }
            return parsedJson;
        }
        catch (e) {
            ctx.addIssue({ code: 'custom', message: 'Invalid JSON' });
            return zod_1.default.NEVER;
        }
    }),
    customProperties: zod_1.default
        .optional(zod_1.default.string())
        .transform((str, ctx) => {
        if ((0, lodash_1.isEmpty)(str))
            return {};
        try {
            return JSON.parse(str);
        }
        catch (e) {
            ctx.addIssue({ code: 'custom', message: 'Invalid JSON' });
            return zod_1.default.NEVER;
        }
    }),
});
//# sourceMappingURL=fields.js.map