"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMedicationRequest = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const utils_1 = require("../../utils");
const extractResourceId_1 = require("../../utils/extractResourceId/extractResourceId");
exports.getMedicationRequest = {
    key: 'getMedicationRequest',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Get medication request',
    description: 'Retrieve medication request details from Medplum',
    fields: config_1.fields,
    previewable: true,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete, onError }) => {
        var _a;
        const { fields: input, medplumSdk } = await (0, utils_1.validateAndCreateSdkClient)({
            fieldsSchema: config_1.FieldsValidationSchema,
            payload,
        });
        const resourceId = (_a = (0, extractResourceId_1.extractResourceId)(input.resourceId, 'MedicationRequest')) !== null && _a !== void 0 ? _a : '';
        const res = await medplumSdk.readResource('MedicationRequest', resourceId);
        const getPatientId = () => {
            var _a, _b;
            const subjectReference = (_b = (_a = res.subject) === null || _a === void 0 ? void 0 : _a.reference) !== null && _b !== void 0 ? _b : '';
            if (subjectReference.startsWith('Patient')) {
                return (0, extractResourceId_1.extractResourceId)(subjectReference, 'Patient');
            }
            return null;
        };
        const getMedicationDisplay = () => {
            var _a, _b, _c, _d, _e;
            if ((_c = (_b = (_a = res.medicationCodeableConcept) === null || _a === void 0 ? void 0 : _a.coding) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.display) {
                return res.medicationCodeableConcept.coding[0].display;
            }
            if ((_d = res.medicationCodeableConcept) === null || _d === void 0 ? void 0 : _d.text) {
                return res.medicationCodeableConcept.text;
            }
            if ((_e = res.medicationReference) === null || _e === void 0 ? void 0 : _e.display) {
                return res.medicationReference.display;
            }
            return '';
        };
        const getDosageInstructions = () => {
            var _a, _b;
            if ((_b = (_a = res.dosageInstruction) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.text) {
                return res.dosageInstruction[0].text;
            }
            return '';
        };
        await onComplete({
            data_points: {
                medicationRequest: JSON.stringify(res),
                status: res.status,
                intent: res.intent,
                priority: res.priority,
                medicationDisplay: getMedicationDisplay(),
                dosageInstructions: getDosageInstructions(),
                patientId: getPatientId(),
            },
        });
    },
};
//# sourceMappingURL=getMedicationRequest.js.map