"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendEmail = void 0;
const lodash_1 = require("lodash");
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const validateAndCreateClient_1 = require("../../lib/validateAndCreateClient");
exports.sendEmail = {
    key: 'sendEmail',
    title: 'Send Email',
    description: 'Send an email to a patient.',
    category: extensions_core_1.Category.COMMUNICATION,
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: false,
    onEvent: async ({ payload, onComplete, onError, helpers }) => {
        const { brazeClient, appId, fields: { externalUserId, from, replyTo: reply_to, subject, body, preheader, shouldInlineCss: should_inline_css, campaignId: campaign_id, messageVariantionId: message_variation_id, }, } = await (0, validateAndCreateClient_1.validateAndCreateClient)({
            fieldsSchema: config_1.FieldsSchema,
            payload,
        });
        const requestBody = {
            external_user_ids: [externalUserId],
            ...(!(0, lodash_1.isNil)(campaign_id) && {
                campaign_id,
            }),
            messages: {
                email: {
                    from,
                    body,
                    app_id: appId,
                    ...(!(0, lodash_1.isNil)(subject) && {
                        subject,
                    }),
                    ...(!(0, lodash_1.isNil)(preheader) && {
                        preheader,
                    }),
                    ...(typeof should_inline_css === 'boolean'
                        ? {
                            should_inline_css,
                        }
                        : {}),
                    ...(!(0, lodash_1.isNil)(message_variation_id) && {
                        message_variation_id,
                    }),
                    ...(!(0, lodash_1.isNil)(reply_to) && {
                        reply_to,
                    }),
                },
            },
        };
        const resp = await brazeClient.sendMessageImmediately(requestBody);
        await onComplete({
            events: [
                {
                    date: new Date().toISOString(),
                    text: { en: `Email sent. Response: ${JSON.stringify(resp)}` },
                },
            ],
            data_points: {
                EmailDispatchId: resp.dispatch_id,
            },
        });
    },
};
//# sourceMappingURL=sendEmail.js.map