"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMessages = void 0;
const zod_1 = require("zod");
const zod_validation_error_1 = require("zod-validation-error");
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");
exports.getMessages = {
    key: 'getMessages',
    title: 'Get messages',
    description: `
    Get a list of text messages matching the given criteria.
    You can also filter the Messages by providing one of the allowed filters.
    `,
    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 }, fields: { recipient, from, date_sent, date_sent_after, date_sent_before, page_size }, } = (0, extensions_core_1.validate)({
                schema: zod_1.z
                    .object({
                    settings: settings_1.SettingsValidationSchema,
                    fields: config_1.FieldsValidationSchema,
                }),
                payload,
            });
            const client = (0, twilio_1.default)(accountSid, authToken, {
                region: 'IE1',
                accountSid,
            });
            const params = {
                to: recipient,
                from,
                dateSent: date_sent,
                dateSentAfter: date_sent_after,
                dateSentBefore: date_sent_before,
                pageSize: page_size
            };
            const messages = await client.messages.list(params);
            const numberOfMessages = messages.length;
            const allMessages = messages.map(function (message) {
                return message.body;
            });
            const latestMessage = numberOfMessages > 0 ? messages[0].body : undefined;
            await onComplete({ data_points: {
                    allMessages: JSON.stringify(allMessages),
                    numberOfMessages: String(numberOfMessages),
                    latestMessage
                } });
        }
        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=getMessages.js.map