"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FieldsValidationSchema = exports.fields = void 0;
const lodash_1 = require("lodash");
const zod_1 = require("zod");
const extensions_core_1 = require("@awell-health/extensions-core");
const awell_1 = require("../../../../../../src/lib/awell");
exports.fields = {
    fromName: {
        id: 'fromName',
        label: 'From name',
        description: 'The name that will be used for the "From" header. When left blank, the value specified in the extension settings will be used.',
        type: extensions_core_1.FieldType.STRING,
        required: false,
    },
    fromEmail: {
        id: 'fromEmail',
        label: 'From email',
        description: 'The email address that will be used for the "From" header. When left blank, the value specified in the extension settings will be used.',
        type: extensions_core_1.FieldType.STRING,
        required: false,
    },
    to: {
        id: 'to',
        label: 'To',
        description: 'The email address of the recipient.',
        type: extensions_core_1.FieldType.STRING,
        required: true,
    },
    templateId: {
        id: 'templateId',
        label: 'Template ID',
        description: 'The immutable ID of a template that exists in your account.',
        type: extensions_core_1.FieldType.STRING,
        required: true,
    },
    subject: {
        id: 'subject',
        label: 'Subject',
        description: 'The subject of your email. If you would like to compose a dynamic subject line then pass a subject field with the template content below.',
        type: extensions_core_1.FieldType.STRING,
        required: false,
    },
    dynamicTemplateData: {
        id: 'dynamicTemplateData',
        label: 'Template content',
        description: 'Specify a JSON blob containing the dynamic data of your template. Read the Sendgrid documentation to learn more about dynamic template data.',
        type: extensions_core_1.FieldType.JSON,
        required: false,
    },
};
exports.FieldsValidationSchema = zod_1.z.object({
    to: (0, awell_1.getEmailValidation)(),
    subject: zod_1.z.optional(zod_1.z.string().transform((str) => ((0, lodash_1.isEmpty)(str) ? undefined : str))),
    templateId: zod_1.z.string(),
    dynamicTemplateData: zod_1.z
        .optional(zod_1.z.string())
        .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)) {
                ctx.addIssue({
                    code: 'custom',
                    message: 'dynamicTemplateData should be an object',
                });
                return zod_1.z.NEVER;
            }
            return parsedJson;
        }
        catch (e) {
            ctx.addIssue({ code: 'custom', message: 'Invalid JSON' });
            return zod_1.z.NEVER;
        }
    }),
    fromName: zod_1.z.string().optional(),
    fromEmail: (0, awell_1.getEmailValidation)().optional(),
});
//# sourceMappingURL=fields.js.map