"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setContactConsent = 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"));
exports.setContactConsent = {
    key: 'setContactConsent',
    title: 'Set Contact Consent',
    description: 'Set contact consent status. You can set consent as true or false. ',
    category: extensions_core_1.Category.COMMUNICATION,
    fields: config_1.fields,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        try {
            const { settings: { accessToken }, fields: { recipient, consentStatus }, } = (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);
            await textLineApi.setContactConsent(consentStatus, recipient);
            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: '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=setContactConsent.js.map