"use strict";
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 extensions_core_1 = require("@awell-health/extensions-core");
const settings_1 = require("../../../settings");
const config_1 = require("./config");
const lodash_1 = require("lodash");
const client_1 = require("../../../client");
exports.sendSms = {
    key: 'sendSms',
    title: 'Send SMS',
    description: 'Send a text message to a recipient of your choice.',
    category: extensions_core_1.Category.COMMUNICATION,
    fields: config_1.fields,
    previewable: false,
    onActivityCreated: async (payload, onComplete, onError) => {
        var _a;
        try {
            const { activity: { id: activityId }, } = payload;
            const { settings: { productToken, fromName: defaultFromName }, fields: { recipient, message, fromName }, } = (0, extensions_core_1.validate)({
                schema: zod_1.z
                    .object({
                    settings: settings_1.SettingsValidationSchema,
                    fields: config_1.FieldsValidationSchema,
                })
                    .superRefine((value, ctx) => {
                    // if both `from` values missing - throw error
                    if ((0, lodash_1.isNil)(value.settings.fromName) &&
                        (0, lodash_1.isNil)(value.fields.fromName)) {
                        ctx.addIssue({
                            code: zod_1.z.ZodIssueCode.custom,
                            fatal: true,
                            message: '"fromName" is missing in both settings and in the action field.',
                        });
                    }
                }),
                payload,
            });
            const client = new client_1.CmClient({
                productToken,
            });
            await client.sendSms({
                // default only for TypeScript check, in reality error will be thrown if both empty
                from: (_a = fromName !== null && fromName !== void 0 ? fromName : defaultFromName) !== null && _a !== void 0 ? _a : '',
                to: recipient,
                message,
                reference: activityId,
            });
            await onComplete();
        }
        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: 'WRONG_INPUT',
                                message: error.message,
                            },
                        },
                    ],
                });
            }
            else if ((0, client_1.isSmsError)(err)) {
                const events = (0, client_1.smsErrorToActivityEvent)(err);
                await onError({ events });
            }
            else {
                const message = err.message;
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: { en: message },
                            error: {
                                category: 'SERVER_ERROR',
                                message,
                            },
                        },
                    ],
                });
            }
        }
    },
};
//# sourceMappingURL=sendSms.js.map