"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getServiceRequest = 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.getServiceRequest = {
    key: 'getServiceRequest',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Get service request',
    description: 'Retrieve service 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, 'ServiceRequest')) !== null && _a !== void 0 ? _a : '';
        const res = await medplumSdk.readResource('ServiceRequest', 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;
        };
        await onComplete({
            data_points: {
                serviceRequestResource: JSON.stringify(res),
                status: res.status,
                intent: res.intent,
                priority: res.priority,
                patientId: getPatientId(),
            },
        });
    },
};
//# sourceMappingURL=getServiceRequest.js.map