"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendEmailWithSingleSendApi = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const lib_1 = require("../../lib");
const config_1 = require("./config");
exports.sendEmailWithSingleSendApi = {
    key: 'sendEmailWithSingleSendApi',
    category: extensions_core_1.Category.CUSTOMER_SUPPORT,
    title: 'Send email (Single Send API)',
    description: 'Send email with the Single Send API',
    fields: config_1.fields,
    previewable: true,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete, onError, helpers }) => {
        helpers.log({ fields: payload.fields }, 'Processing sendEmailWithSingleSendApi');
        try {
            const { hubSpotSdk, fields, activity } = await (0, lib_1.validatePayloadAndCreateSdks)({
                fieldsSchema: config_1.FieldsValidationSchema,
                payload,
            });
            const requestBody = {
                emailId: Number(fields.emailId),
                message: {
                    _from: fields.from,
                    to: fields.to,
                    sendId: `awell_activity_${activity.id}`,
                },
                contactProperties: fields.contactProperties,
                customProperties: fields.customProperties,
            };
            helpers.log({ requestBody }, 'Sending email via HubSpot Single Send API');
            const res = await hubSpotSdk.marketing.transactional.singleSendApi.sendEmail(requestBody);
            await onComplete({
                data_points: {
                    statusId: res.statusId,
                },
            });
        }
        catch (err) {
            helpers.log({ err }, 'error', err);
            const error = err;
            await onError({
                events: [
                    {
                        date: new Date().toISOString(),
                        text: { en: error.message },
                        error: {
                            category: 'SERVER_ERROR',
                            message: error.message,
                        },
                    },
                ],
            });
        }
    },
};
//# sourceMappingURL=sendEmailWithSingleSendApi.js.map