"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPatient = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const client_1 = require("../../client");
const errorHandler_1 = require("../../shared/errorHandler");
const fields_1 = require("./fields");
const generic_zod_1 = require("../../validation/generic.zod");
const dataPoints_1 = require("./dataPoints");
const lodash_1 = require("lodash");
exports.getPatient = {
    key: 'getPatient',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Get Patient',
    description: 'Gets a Patient.',
    fields: fields_1.getFields,
    previewable: true,
    dataPoints: dataPoints_1.patientDataPoints,
    onActivityCreated: async (payload, onComplete, onError) => {
        var _a, _b;
        try {
            const patientId = generic_zod_1.stringId.parse(payload.fields.patientId);
            const api = (0, client_1.createMetriportApi)(payload.settings);
            const patient = await api.getPatient(patientId);
            if ((0, lodash_1.isNil)(patient.personalIdentifiers)) {
                throw new Error('Patient does not have any personal identifiers.');
            }
            if (Array.isArray(patient.address)) {
                patient.address = patient.address[0];
            }
            if (Array.isArray(patient.contact)) {
                patient.contact = patient.contact[0];
            }
            const driversLicense = patient.personalIdentifiers.find((id) => id.type === 'driversLicense');
            await onComplete({
                data_points: {
                    firstName: patient.firstName,
                    lastName: patient.lastName,
                    dob: patient.dob,
                    genderAtBirth: patient.genderAtBirth,
                    driversLicenseValue: driversLicense === null || driversLicense === void 0 ? void 0 : driversLicense.value,
                    driversLicenseState: driversLicense === null || driversLicense === void 0 ? void 0 : driversLicense.state,
                    addressLine1: patient.address.addressLine1,
                    addressLine2: patient.address.addressLine2,
                    city: patient.address.city,
                    state: patient.address.state,
                    zip: patient.address.zip,
                    country: patient.address.country,
                    phone: (_a = patient.contact) === null || _a === void 0 ? void 0 : _a.phone,
                    email: (_b = patient.contact) === null || _b === void 0 ? void 0 : _b.email,
                },
            });
        }
        catch (err) {
            await (0, errorHandler_1.handleErrorMessage)(err, onError);
        }
    },
};
//# sourceMappingURL=get.js.map