"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendEmailUsingTemplate = void 0;
const lodash_1 = require("lodash");
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const actionHelpers_1 = require("../actionHelpers");
const validateAndCreateClient_1 = require("../../lib/validateAndCreateClient");
exports.sendEmailUsingTemplate = {
    key: 'sendEmailUsingTemplate',
    title: 'Send Email Using Template',
    description: 'Send an email to a patient using a template. You can schedule the email to be sent at a later time.',
    category: extensions_core_1.Category.COMMUNICATION,
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onEvent: async ({ payload, onComplete, onError, helpers }) => {
        const { brazeClient, appId, fields: { externalUserId, from, replyTo: reply_to, templateId: email_template_id, preheader, scheduleTime: time, inPatientLocalTime: in_local_time, atOptimalTime: at_optimal_time, campaignId: campaign_id, messageVariantionId: message_variation_id, }, } = await (0, validateAndCreateClient_1.validateAndCreateClient)({
            fieldsSchema: config_1.FieldsSchema,
            payload,
        });
        const schedule = {
            ...(!(0, lodash_1.isNil)(time) && {
                time,
            }),
            ...(0, actionHelpers_1.addBooleanFieldIfDefined)({ in_local_time }),
            ...(0, actionHelpers_1.addBooleanFieldIfDefined)({ at_optimal_time }),
        };
        const requestBody = {
            external_user_ids: [externalUserId],
            ...(!(0, lodash_1.isNil)(campaign_id) && {
                campaign_id,
            }),
            ...(!(0, lodash_1.isEmpty)(schedule) && {
                schedule,
            }),
            messages: {
                email: {
                    from,
                    app_id: appId,
                    email_template_id,
                    ...(!(0, lodash_1.isNil)(preheader) && {
                        preheader,
                    }),
                    ...(!(0, lodash_1.isNil)(message_variation_id) && {
                        message_variation_id,
                    }),
                    ...(!(0, lodash_1.isNil)(reply_to) && {
                        reply_to,
                    }),
                },
            },
        };
        const resp = (0, lodash_1.isNil)(time)
            ? await brazeClient.sendMessageImmediately(requestBody)
            : await brazeClient.scheduleMessage(requestBody);
        await onComplete({
            events: [
                {
                    date: new Date().toISOString(),
                    text: {
                        en: `Email sent using template id ${email_template_id}. Response: ${JSON.stringify(resp)}`,
                    },
                },
            ],
            data_points: {
                EmailDispatchId: resp.dispatch_id,
            },
        });
    },
};
//# sourceMappingURL=sendEmailUsingTemplate.js.map