"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendCampaign = 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.sendCampaign = {
    key: 'sendCampaign',
    title: 'Send campaign',
    description: 'Send a campaign with Braze',
    category: extensions_core_1.Category.COMMUNICATION,
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onEvent: async ({ payload, onComplete, onError, helpers }) => {
        const { brazeClient, fields: { externalUserId: external_user_id, sendId: send_id, email, campaignId: campaign_id, triggerProperties: trigger_properties, attributes, }, } = await (0, validateAndCreateClient_1.validateAndCreateClient)({
            fieldsSchema: config_1.FieldsSchema,
            payload,
            requiresAppId: false, // Sending a campaign doesn't require an app ID to be set
        });
        if ((0, lodash_1.isNil)(external_user_id) && (0, lodash_1.isNil)(email)) {
            throw new Error('Either externalUserId or email must be provided.');
        }
        const requestBody = {
            campaign_id,
            recipients: [
                {
                    ...(!(0, lodash_1.isNil)(external_user_id) && {
                        external_user_id,
                    }),
                    ...(!(0, lodash_1.isNil)(email) && {
                        email,
                    }),
                    ...(!(0, lodash_1.isNil)(trigger_properties) && {
                        trigger_properties,
                    }),
                    ...(!(0, lodash_1.isNil)(attributes) && {
                        attributes,
                    }),
                },
            ],
            ...(!(0, lodash_1.isNil)(send_id) && {
                send_id,
            }),
        };
        const resp = await brazeClient.sendCampaign(requestBody);
        await onComplete({
            events: [
                {
                    date: new Date().toISOString(),
                    text: { en: `Email sent. Response: ${JSON.stringify(resp)}` },
                },
            ],
            data_points: {
                EmailDispatchId: resp.dispatch_id,
            },
        });
    },
};
//# sourceMappingURL=sendCampaign.js.map