"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendWhatsAppMessage = void 0;
const config_1 = require("./config");
const extensions_core_1 = require("@awell-health/extensions-core");
const lodash_1 = require("lodash");
const messagebirdSdk_1 = __importDefault(require("../../../common/sdk/messagebirdSdk"));
exports.sendWhatsAppMessage = {
    key: 'sendWhatsAppMessage',
    title: 'Send WhatsApp message',
    description: 'Send a WhatsApp message to a recipient of your choice.',
    category: extensions_core_1.Category.COMMUNICATION,
    fields: config_1.fields,
    previewable: false,
    onActivityCreated: async (payload, onComplete, onError) => {
        const { fields: { from, to, content }, settings: { apiKey, reportUrl }, } = payload;
        try {
            const allRequiredFieldsHaveValues = [from, to, content].every((field) => !(0, lodash_1.isEmpty)(field));
            if (!allRequiredFieldsHaveValues) {
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: { en: 'Fields are missing' },
                            error: {
                                category: 'MISSING_FIELDS',
                                message: '`from`, `to`, or `content` is missing',
                            },
                        },
                    ],
                });
                return;
            }
            if ((0, lodash_1.isNil)(apiKey)) {
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: { en: 'Missing an API key' },
                            error: {
                                category: 'MISSING_SETTINGS',
                                message: 'Missing an API key',
                            },
                        },
                    ],
                });
                return;
            }
            (0, messagebirdSdk_1.default)(apiKey).conversations.send({
                to: String(to),
                from: String(from),
                type: 'text',
                content: {
                    text: String(content),
                },
                reportUrl: String(reportUrl),
            }, function (error, response) {
                if (error != null) {
                    void onError({
                        events: [
                            {
                                date: new Date().toISOString(),
                                text: { en: 'Exception when calling the MessageBird API' },
                                error: {
                                    category: 'SERVER_ERROR',
                                    message: error.message,
                                },
                            },
                        ],
                    });
                }
                else {
                    void onComplete();
                }
            });
        }
        catch (err) {
            const error = err;
            await onError({
                events: [
                    {
                        date: new Date().toISOString(),
                        text: { en: 'Something went wrong while orchestration the action' },
                        error: {
                            category: 'SERVER_ERROR',
                            message: error.message,
                        },
                    },
                ],
            });
        }
    },
};
//# sourceMappingURL=sendWhatsAppMessage.js.map