"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createClinicalNote = 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");
const DocumentReference_1 = require("../../lib/api/customFhirSchemas/DocumentReference");
exports.createClinicalNote = {
    key: 'createClinicalNote',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Create clinical note',
    description: 'Create a clinical note in Epic',
    fields: config_1.fields,
    previewable: false,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete, onError }) => {
        var _a, _b;
        const { epicFhirR4Sdk, fields: { patientResourceId, encounterResourceId, status, type, note }, } = await (0, validatePayloadAndCreateSdks_1.validatePayloadAndCreateSdks)({
            fieldsSchema: config_1.FieldsValidationSchema,
            payload,
        });
        const DocumentReferenceInput = {
            resourceType: 'DocumentReference',
            docStatus: status,
            type: {
                coding: [
                    {
                        system: 'http://loinc.org',
                        code: DocumentReference_1.LoincCodeTypesDictionary[type].code,
                        display: DocumentReference_1.LoincCodeTypesDictionary[type].label,
                    },
                ],
                text: DocumentReference_1.LoincCodeTypesDictionary[type].label,
            },
            subject: {
                reference: `Patient/${patientResourceId}`,
            },
            content: [
                {
                    attachment: {
                        contentType: 'text/plain',
                        data: Buffer.from(note, 'utf-8').toString('base64'),
                    },
                },
            ],
            context: {
                encounter: [
                    {
                        reference: `Encounter/${encounterResourceId}`,
                    },
                ],
            },
        };
        try {
            const res = await epicFhirR4Sdk.createDocumentReference(DocumentReferenceInput);
            const resourceReference = (_a = res.headers.Location) !== null && _a !== void 0 ? _a : res.headers.location;
            const resourceId = resourceReference.split('/')[1];
            await onComplete({
                data_points: {
                    resourceId,
                },
            });
        }
        catch (error) {
            if (error instanceof axios_1.AxiosError) {
                const err = error;
                await onError({
                    events: [
                        (0, awell_1.addActivityEventLog)({
                            message: JSON.stringify((_b = err.response) === null || _b === void 0 ? void 0 : _b.data, null, 2),
                        }),
                    ],
                });
                return;
            }
            // Throw all other errors
            throw error;
        }
    },
};
//# sourceMappingURL=createClinicalNote.js.map