"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");
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, _c;
        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);
            const address = Array.isArray(patient.address)
                ? patient.address[0]
                : patient.address;
            const contact = Array.isArray(patient.contact)
                ? patient.contact[0]
                : patient.contact;
            const driversLicense = ((_a = patient.personalIdentifiers) !== null && _a !== void 0 ? _a : []).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: address === null || address === void 0 ? void 0 : address.addressLine1,
                    addressLine2: address === null || address === void 0 ? void 0 : address.addressLine2,
                    city: address === null || address === void 0 ? void 0 : address.city,
                    state: address === null || address === void 0 ? void 0 : address.state,
                    zip: address === null || address === void 0 ? void 0 : address.zip,
                    country: address === null || address === void 0 ? void 0 : address.country,
                    phone: (_b = contact === null || contact === void 0 ? void 0 : contact.phone) !== null && _b !== void 0 ? _b : undefined,
                    email: (_c = contact === null || contact === void 0 ? void 0 : contact.email) !== null && _c !== void 0 ? _c : undefined,
                },
            });
        }
        catch (err) {
            await (0, errorHandler_1.handleErrorMessage)(err, onError);
        }
    },
};
//# sourceMappingURL=get.js.map