"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendSms = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const validatePayloadAndCreateSdk_1 = require("../../lib/validatePayloadAndCreateSdk");
const config_1 = require("./config");
const addEventLog_1 = require("../../../../src/lib/awell/addEventLog");
const codes = [
    {
        code: 'INVALID_CONSUMER_NUMBER_FORMAT',
        errorMessage: 'Invalid consumer number. The format of the consumer number used is incorrect. E.164 format is required',
    },
    {
        code: 'CONTACT_CENTER_NUMBER_USED',
        errorMessage: 'Zoom Contact Center numbers cannot be used as consumer numbers',
    },
    {
        code: 'CONSUMER_NOT_OPTED_IN',
        errorMessage: 'The consumer number you have messaged has not opted in',
    },
    {
        code: 'CONSUMER_OPTED_OUT',
        errorMessage: 'This consumer number has opted out of receiving SMS from this Zoom contact center number',
    },
    {
        code: 'CONSUMER_BLOCK_LISTED',
        errorMessage: 'This consumer number is a part of the block list defined by your Zoom Contact Center account administrator and cannot be messaged',
    },
    {
        code: 'INTERNATIONAL_SMS_NOT_SUPPORTED',
        errorMessage: 'International messaging is not supported on this account',
    },
    // Keep non-enum validations we have seen from API responses
    {
        code: 'BODY_TOO_LONG',
        errorMessage: 'Validation error: String must contain at most 500 character(s)',
    },
];
exports.sendSms = {
    key: 'sendSms',
    category: extensions_core_1.Category.COMMUNICATION,
    title: 'Send SMS',
    description: 'Send an SMS',
    fields: config_1.fields,
    previewable: false,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete, onError }) => {
        var _a, _b, _c;
        const { fields, zoomSdk } = await (0, validatePayloadAndCreateSdk_1.validatePayloadAndCreateSdk)({
            fieldsSchema: config_1.FieldsValidationSchema,
            payload,
        });
        const { data } = await zoomSdk.sendSms({
            contact_center_number: fields.contactCenterNumber,
            consumer_numbers: [fields.to],
            body: fields.body,
        });
        /**
         * Custom error handling for Zoom API
         * For some errors, Zoom returns a 200 with success: false
         *
         * All other errors are handled with AxiosError and caught in Extensions Core
         */
        if (!data[0].success) {
            const failureDescription = (_a = data[0].description) !== null && _a !== void 0 ? _a : 'Unknown error';
            const code = (_b = codes.find((code) => failureDescription.includes(code.errorMessage))) === null || _b === void 0 ? void 0 : _b.code;
            if (code !== undefined) {
                await onComplete({
                    data_points: {
                        code,
                        success: String(false),
                        messageId: null,
                    },
                    events: [
                        (0, addEventLog_1.addActivityEventLog)({
                            message: failureDescription,
                        }),
                    ],
                });
            }
            else {
                await onError({
                    events: [
                        (0, addEventLog_1.addActivityEventLog)({
                            message: (_c = data[0].description) !== null && _c !== void 0 ? _c : 'Unknown error',
                        }),
                    ],
                });
            }
            return;
        }
        await onComplete({
            data_points: {
                messageId: data[0].message_id,
                success: String(data[0].success),
                code: 'SUCCESS',
            },
            events: [
                (0, addEventLog_1.addActivityEventLog)({
                    message: `Success: ${String(data[0].success)}\nMessage ID: ${String(data[0].message_id)}\nDescription: ${String(data[0].description)}`,
                }),
            ],
        });
    },
};
//# sourceMappingURL=sendSms.js.map