"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.addClinicalDocument = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const helpers_1 = require("../../helpers");
const htmlToPdf_1 = require("../../../../src/utils/htmlToPdf");
exports.addClinicalDocument = {
    key: 'addClinicalDocument',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Add clinical document to chart',
    description: 'Add clinical document to patient chart',
    fields: config_1.fields,
    previewable: false,
    dataPoints: config_1.dataPoints,
    onActivityCreated: async (payload, onComplete, onError) => {
        var _a;
        const { fields: input, client, settings: { practiceId }, } = await (0, helpers_1.validatePayloadAndCreateClient)({
            fieldsSchema: config_1.FieldsValidationSchema,
            payload,
        });
        const contentAsBase64String = await (0, htmlToPdf_1.htmlToBase64Pdf)((_a = input.attachmentcontents) !== null && _a !== void 0 ? _a : '');
        const res = await client.addClinicalDocumentToPatientChart({
            practiceId,
            patientId: input.patientid,
            data: {
                departmentid: input.departmentid,
                documentsubclass: 'CLINICALDOCUMENT',
                attachmentcontents: contentAsBase64String,
            },
        });
        if (res.success) {
            await onComplete({
                data_points: {
                    clinicalDocumentId: String(res.clinicaldocumentid),
                },
            });
            return;
        }
        await onError({
            events: [
                {
                    date: new Date().toISOString(),
                    text: {
                        en: 'Unable to add the clinical document to the patient chart',
                    },
                    error: {
                        category: 'SERVER_ERROR',
                        message: 'Unable to add the clinical document to the patient chart',
                    },
                },
            ],
        });
    },
};
//# sourceMappingURL=addClinicalDocument.js.map