"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getClinicalNote = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const validatePayloadAndCreateSdks_1 = require("../../lib/validatePayloadAndCreateSdks");
const axios_1 = require("axios");
const awell_1 = require("../../../../src/lib/awell");
exports.getClinicalNote = {
    key: 'getClinicalNote',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Get clinical note',
    description: 'Retrieve clinical note details from Epic',
    fields: config_1.fields,
    previewable: true,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete, onError }) => {
        var _a, _b, _c, _d;
        const { epicFhirR4Sdk, fields: { resourceId }, } = await (0, validatePayloadAndCreateSdks_1.validatePayloadAndCreateSdks)({
            fieldsSchema: config_1.FieldsValidationSchema,
            payload,
        });
        try {
            const res = await epicFhirR4Sdk.getDocumentReference(resourceId);
            const binaryContentHtml = res.data.content.find(content => content.attachment.contentType === 'text/html');
            const binaryContentRtf = res.data.content.find(content => content.attachment.contentType === 'text/rtf');
            const binaryIdHtml = (_b = (_a = binaryContentHtml === null || binaryContentHtml === void 0 ? void 0 : binaryContentHtml.attachment) === null || _a === void 0 ? void 0 : _a.url) === null || _b === void 0 ? void 0 : _b.split('/')[1];
            const binaryIdRtf = (_d = (_c = binaryContentRtf === null || binaryContentRtf === void 0 ? void 0 : binaryContentRtf.attachment) === null || _c === void 0 ? void 0 : _c.url) === null || _d === void 0 ? void 0 : _d.split('/')[1];
            const getBinary = async (binaryId) => {
                if (binaryId === undefined) {
                    return undefined;
                }
                const binaryRes = await epicFhirR4Sdk.getBinary(binaryId);
                return binaryRes.data;
            };
            const binaryHtml = await getBinary(binaryIdHtml);
            const binaryRtf = await getBinary(binaryIdRtf);
            await onComplete({
                data_points: {
                    documentReference: JSON.stringify(res.data),
                    binaryContentHtml: binaryHtml !== undefined ? JSON.stringify(binaryHtml) : "Binary not found",
                    binaryContentRtf: binaryRtf !== undefined ? JSON.stringify(binaryRtf) : "Binary not found",
                },
            });
        }
        catch (error) {
            if (error instanceof axios_1.AxiosError) {
                const err = error;
                if (err.status === 404)
                    await onError({
                        events: [
                            (0, awell_1.addActivityEventLog)({
                                message: 'Document reference not found',
                            }),
                        ],
                    });
                return;
            }
            // Throw all other errors
            throw error;
        }
    },
};
//# sourceMappingURL=getClinicalNote.js.map