"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.scheduleSMS = 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.scheduleSMS = {
    key: 'scheduleSMS',
    title: 'Schedule SMS',
    description: 'Schedule an SMS to be sent immediateky 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: { subscriptionGroupId: subscription_group_id, externalUserId, body, scheduleTime: time, inPatientLocalTime: in_local_time, atOptimalTime: at_optimal_time, 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,
            }),
            schedule: {
                time,
                ...(0, actionHelpers_1.addBooleanFieldIfDefined)({ in_local_time }),
                ...(0, actionHelpers_1.addBooleanFieldIfDefined)({ at_optimal_time }),
            },
            messages: {
                sms: {
                    subscription_group_id,
                    body,
                    app_id: appId,
                    ...(!(0, lodash_1.isNil)(message_variation_id) && {
                        message_variation_id,
                    }),
                    ...(0, actionHelpers_1.addBooleanFieldIfDefined)({ link_shortening_enabled }),
                    ...(0, actionHelpers_1.addBooleanFieldIfDefined)({
                        use_click_tracking_enabled,
                    }),
                },
            },
        };
        const resp = await brazeClient.scheduleMessage(requestBody);
        await onComplete({
            events: [
                {
                    date: new Date().toISOString(),
                    text: { en: `SMS scheduled. Response: ${JSON.stringify(resp)}` },
                },
            ],
            data_points: {
                SMSScheduleId: resp.schedule_id,
                SMSDispatchId: resp.dispatch_id,
            },
        });
    },
};
//# sourceMappingURL=scheduleSMS.js.map