"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendSMS = 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.sendSMS = {
    key: 'sendSMS',
    title: 'Send SMS',
    description: 'Send an SMS to a patient.',
    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: { subscriptionGroupId: subscription_group_id, externalUserId, body, linkShorteningEnabled: link_shortening_enabled, useClickTrackingEnabled: use_click_tracking_enabled, 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: {
                sms: {
                    subscription_group_id,
                    body,
                    app_id: appId,
                    ...(!(0, lodash_1.isNil)(message_variation_id) && {
                        message_variation_id,
                    }),
                    ...(typeof link_shortening_enabled === 'boolean'
                        ? {
                            link_shortening_enabled,
                        }
                        : {}),
                    ...(typeof use_click_tracking_enabled === 'boolean'
                        ? {
                            use_click_tracking_enabled,
                        }
                        : {}),
                },
            },
        };
        const resp = await brazeClient.sendMessageImmediately(requestBody);
        await onComplete({
            events: [
                {
                    date: new Date().toISOString(),
                    text: { en: `SMS sent. Response: ${JSON.stringify(resp)}` },
                },
            ],
            data_points: {
                SMSDispatchId: resp.dispatch_id,
            },
        });
    },
};
//# sourceMappingURL=sendSMS.js.map