"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.addSuppressions = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const client_1 = require("../../../client");
const settings_1 = require("../../../settings");
const lodash_1 = require("lodash");
const config_1 = require("./config");
const zod_1 = require("zod");
const zod_validation_error_1 = require("zod-validation-error");
const classes_1 = require("@sendgrid/helpers/classes");
exports.addSuppressions = {
    key: 'addSuppressions',
    category: extensions_core_1.Category.COMMUNICATION,
    title: 'Add Email to Suppression Lists',
    description: 'Adds an email to one or more suppression lists.',
    fields: config_1.fields,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        try {
            const { fields: { email, groups = [] }, settings: { apiKey }, } = (0, extensions_core_1.validate)({
                schema: zod_1.z.object({
                    fields: config_1.FieldsValidationSchema,
                    settings: settings_1.SettingsValidationSchema,
                }),
                payload,
            });
            const sendgridClient = new client_1.SendgridClient({ apiKey });
            if (!(0, lodash_1.isNil)(email) && !(0, lodash_1.isEmpty)(email)) {
                const tasks = groups === null || groups === void 0 ? void 0 : groups.map(async (g) => {
                    return await sendgridClient.groups.suppressions.add(g, email);
                });
                await Promise.all(tasks);
                await onComplete();
            }
            else {
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: {
                                en: 'There was in error attempting to unenroll user from Sendgrid',
                            },
                            error: {
                                category: 'SERVER_ERROR',
                                message: 'Email cannot be blank',
                            },
                        },
                    ],
                });
            }
        }
        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.name },
                            error: {
                                category: 'WRONG_INPUT',
                                message: `${error.message}`,
                            },
                        },
                    ],
                });
                return;
            }
            else if (err instanceof classes_1.ResponseError) {
                const events = (0, client_1.mapSendgridErrorsToActivityErrors)(err);
                await onError({ events });
                return;
            }
            const error = err;
            await onError({
                events: [
                    {
                        date: new Date().toISOString(),
                        text: { en: 'There was an error in processing the suppression' },
                        error: {
                            category: 'SERVER_ERROR',
                            message: error.message,
                        },
                    },
                ],
            });
        }
    },
};
//# sourceMappingURL=addSuppressions.js.map