"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLetter = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const client_1 = require("../../client");
const config_1 = require("./config");
exports.getLetter = {
    key: 'getLetter',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Get letter',
    description: 'Retrieve a letter from Elation.',
    fields: config_1.fields,
    previewable: true,
    supports_automated_retries: true,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete, onError, helpers }) => {
        try {
            const { letterId } = config_1.FieldsValidationSchema.parse(payload.fields);
            const api = (0, client_1.makeAPIClient)(payload.settings);
            helpers.log({ letterId }, 'Getting Elation letter');
            const res = await api.getLetter(letterId);
            helpers.log({ letterId }, 'Got Elation letter');
            await onComplete({
                data_points: {
                    body: res.body,
                    signedBy: typeof (res === null || res === void 0 ? void 0 : res.signed_by) === 'number'
                        ? String(res.signed_by)
                        : undefined,
                },
            });
        }
        catch (err) {
            const message = err.message;
            helpers.log({ err }, 'error', err);
            await onError({
                events: [
                    {
                        date: new Date().toISOString(),
                        text: { en: message },
                        error: {
                            category: 'SERVER_ERROR',
                            message,
                        },
                    },
                ],
            });
        }
    },
};
//# sourceMappingURL=getLetter.js.map