"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendSmsWithMessagingService = void 0;
const zod_1 = require("zod");
const twilio_1 = __importDefault(require("../../../common/sdk/twilio"));
const extensions_core_1 = require("@awell-health/extensions-core");
const settings_1 = require("../../../settings");
const config_1 = require("./config");
const lodash_1 = require("lodash");
const lib_1 = require("../../../lib");
const errors_1 = require("../../../lib/errors");
exports.sendSmsWithMessagingService = {
    key: 'sendSmsWithMessagingService',
    title: 'Send SMS (with Messaging Service)',
    description: 'Send a text message from a Messaging Service to a recipient of your choice.',
    category: extensions_core_1.Category.COMMUNICATION,
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        try {
            const { settings: { accountSid, authToken, messagingServiceSid: defaultMessagingServiceSid, addOptOutLanguage, optOutLanguage, language, }, fields: { recipient, message, messagingServiceSid }, } = (0, extensions_core_1.validate)({
                schema: zod_1.z
                    .object({
                    settings: settings_1.SettingsValidationSchema,
                    fields: config_1.FieldsValidationSchema,
                })
                    .superRefine((value, ctx) => {
                    // if both `from` values missing - throw error
                    if ((0, lodash_1.isNil)(value.settings.messagingServiceSid) &&
                        (0, lodash_1.isNil)(value.fields.messagingServiceSid)) {
                        ctx.addIssue({
                            code: zod_1.z.ZodIssueCode.custom,
                            fatal: true,
                            message: '"Messaging Service SID" is missing in both settings and in the action field.',
                        });
                    }
                }),
                payload,
            });
            const client = (0, twilio_1.default)(accountSid, authToken, {
                region: 'IE1',
                accountSid,
            });
            const { sid } = await client.messages.create({
                body: addOptOutLanguage
                    ? (0, lib_1.appendOptOutLanguage)(message, optOutLanguage, language)
                    : message,
                messagingServiceSid: messagingServiceSid !== null && messagingServiceSid !== void 0 ? messagingServiceSid : defaultMessagingServiceSid,
                to: recipient,
            });
            const messageSidLog = `Message SID: ${sid}`;
            await onComplete({
                data_points: {
                    messageSid: sid,
                },
                events: [
                    {
                        date: new Date().toISOString(),
                        text: {
                            en: messageSidLog,
                        },
                    },
                ],
            });
        }
        catch (error) {
            if ((0, errors_1.isTwilioErrorResponse)(error)) {
                await onError({
                    events: [(0, errors_1.parseTwilioError)(error)],
                });
            }
            else {
                throw error;
            }
        }
    },
};
//# sourceMappingURL=sendSmsWithMessagingService.js.map