"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.scheduleEmail = 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.scheduleEmail = {
    key: 'scheduleEmail',
    title: 'Schedule Email',
    description: 'Schedule an email to be sent 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, 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 requestBody = {
            external_user_ids: [externalUserId],
            ...(!(0, lodash_1.isNil)(campaign_id) && {
                campaign_id,
            }),
            schedule: {
                time,
                ...(0, actionHelpers_1.addBooleanFieldIfDefined)({ in_local_time }),
                ...(0, actionHelpers_1.addBooleanFieldIfDefined)({ at_optimal_time }),
            },
            messages: {
                email: {
                    from,
                    body,
                    app_id: appId,
                    ...(!(0, lodash_1.isNil)(subject) && {
                        subject,
                    }),
                    ...(!(0, lodash_1.isNil)(preheader) && {
                        preheader,
                    }),
                    ...(!(0, lodash_1.isNil)(message_variation_id) && {
                        message_variation_id,
                    }),
                    ...(!(0, lodash_1.isNil)(reply_to) && {
                        reply_to,
                    }),
                    ...(0, actionHelpers_1.addBooleanFieldIfDefined)({ should_inline_css }),
                },
            },
        };
        const resp = await brazeClient.scheduleMessage(requestBody);
        await onComplete({
            events: [
                {
                    date: new Date().toISOString(),
                    text: { en: `Email sent. Response: ${JSON.stringify(resp)}` },
                },
            ],
            data_points: {
                EmailDispatchId: resp.dispatch_id,
            },
        });
    },
};
//# sourceMappingURL=scheduleEmail.js.map