"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendSms = void 0;
const zod_1 = require("zod");
const zod_validation_error_1 = require("zod-validation-error");
const settings_1 = require("../../settings");
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const textLineApi_1 = __importDefault(require("../../client/textLineApi"));
const addEventLog_1 = require("../../../../src/lib/awell/addEventLog");
exports.sendSms = {
    key: 'sendSms',
    title: 'Send SMS',
    description: 'Send a text message from a given telephone number to a recipient of your choice.',
    category: extensions_core_1.Category.COMMUNICATION,
    fields: config_1.fields,
    previewable: true,
    dataPoints: config_1.dataPoints,
    onActivityCreated: async (payload, onComplete, onError) => {
        try {
            const { settings: { accessToken }, fields: { recipient, message, departmentId }, } = (0, extensions_core_1.validate)({
                schema: zod_1.z.object({
                    settings: settings_1.SettingsValidationSchema,
                    fields: config_1.FieldsValidationSchema,
                }),
                payload,
            });
            const textLineApi = new textLineApi_1.default(accessToken);
            const response = await textLineApi.sendMessage(message, recipient, departmentId);
            const conversationId = response.post.conversation_uuid;
            const messageId = response.post.uuid;
            await onComplete({
                data_points: {
                    conversationId,
                    messageId,
                },
                events: [
                    (0, addEventLog_1.addActivityEventLog)({
                        message: `TextLine accepted the request to send the message. Conversation ID: ${conversationId}, Message ID: ${messageId}.`,
                    }),
                ],
            });
        }
        catch (err) {
            if (err instanceof zod_1.ZodError) {
                const error = (0, zod_validation_error_1.fromZodError)(err);
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: { en: error.message },
                            error: {
                                category: 'BAD_REQUEST',
                                message: error.message,
                            },
                        },
                    ],
                });
            }
            else {
                const message = err.message;
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: { en: message },
                            error: {
                                category: 'SERVER_ERROR',
                                message,
                            },
                        },
                    ],
                });
            }
        }
    },
};
//# sourceMappingURL=sendSms.js.map