"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSMSBroadcast = void 0;
const zod_1 = require("zod");
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const settings_1 = require("../../settings");
const lib_1 = require("../../lib");
exports.createSMSBroadcast = {
    key: 'createSMSBroadcast',
    title: 'Send a new SMS broadcast',
    description: 'A broadcast is a feature that allows you to send a text message to a recipient at specific time',
    category: extensions_core_1.Category.COMMUNICATION,
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onEvent: async ({ payload, onComplete, onError, helpers }) => {
        const { fields: { broadcastName: BroadcastName, phoneNumber, textMessage: TextMessage, textNumberID: TextNumberID, startDate: StartDate, }, settings, } = (0, extensions_core_1.validate)({
            schema: zod_1.z.object({ fields: config_1.FieldsSchema, settings: settings_1.SettingsSchema }),
            payload,
        });
        const client = new lib_1.TextEmAllClient(settings);
        const request = {
            BroadcastName,
            BroadcastType: 'SMS',
            TextMessage,
            TextNumberID,
            StartDate,
            Contacts: [{ PrimaryPhone: phoneNumber }],
        };
        const resp = await client.createBroadcast(request);
        const data = resp.data;
        if (resp.status === 200 && 'BroadcastID' in data) {
            await onComplete({
                events: [
                    {
                        date: new Date().toISOString(),
                        text: {
                            en: `Broadcast created. Response: ${JSON.stringify(data)}`,
                        },
                    },
                ],
                data_points: {
                    broadcastId: data.BroadcastID.toString(),
                    broadcastStatus: data.BroadcastStatus,
                    uriBroadcastDetails: data.UriBroadcastDetails,
                    broadcastStatusCategory: data.BroadcastStatusCategory,
                    broadcastStartDate: data.StartDate,
                },
            });
        }
        else if (resp.status === 400) {
            const errorMessage = 'Message' in data ? data.Message : 'Unknown error';
            await onError({
                events: [
                    {
                        date: new Date().toISOString(),
                        text: { en: `Broadcast creation failed: ${errorMessage}` },
                        error: {
                            category: 'BAD_REQUEST',
                            message: 'Broadcast creation failed',
                        },
                    },
                ],
            });
        }
        else {
            await onError({
                events: [
                    {
                        date: new Date().toISOString(),
                        text: { en: 'Broadcast creation failed' },
                        error: {
                            category: 'SERVER_ERROR',
                            message: 'Broadcast creation failed',
                        },
                    },
                ],
            });
        }
    },
};
//# sourceMappingURL=createSMSBroadcast.js.map