"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendEmail = void 0;
const zod_1 = require("zod");
const lodash_1 = require("lodash");
const extensions_core_1 = require("@awell-health/extensions-core");
const settings_1 = require("../../settings");
const client_1 = require("../../client");
const config_1 = require("./config");
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 { fields: { externalUserId, appId: app_id, from, replyTo: reply_to, subject, body, preheader, shouldInlineCss: should_inline_css, campaignId: campaign_id, messageVariantionId: message_variation_id, }, settings: { apiKey, baseUrl }, } = (0, extensions_core_1.validate)({
            schema: zod_1.z.object({
                fields: config_1.FieldsSchema,
                settings: settings_1.SettingsValidationSchema,
            }),
            payload,
        });
        const client = new client_1.BrazeClient({ apiKey, baseUrl });
        const requestBody = {
            external_user_ids: [externalUserId],
            ...(!(0, lodash_1.isNil)(campaign_id) && {
                campaign_id,
            }),
            messages: {
                email: {
                    from,
                    body,
                    app_id,
                    ...(!(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 client.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