"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 config_1 = require("./config");
const elationMobilePhoneToE164_1 = require("../../utils/elationMobilePhoneToE164");
const getLastEmail_1 = require("../../utils/getLastEmail");
exports.getPatient = {
    key: 'getPatient',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Get Patient',
    description: 'Retrieve a patient profile using Elation`s patient API.',
    fields: config_1.fields,
    previewable: true,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete }) => {
        var _a, _b, _c, _d;
        const fields = config_1.FieldsValidationSchema.parse(payload.fields);
        // API Call should produce AuthError or something dif.
        const api = (0, client_1.makeAPIClient)(payload.settings);
        const patientInfo = await api.getPatient(fields.patientId);
        await onComplete({
            data_points: {
                firstName: patientInfo.first_name,
                lastName: patientInfo.last_name,
                dob: patientInfo.dob,
                sex: patientInfo.sex,
                primaryPhysicianId: String(patientInfo.primary_physician),
                caregiverPracticeId: String(patientInfo.caregiver_practice),
                mainPhone: (0, elationMobilePhoneToE164_1.elationMobilePhoneToE164)((_b = (_a = patientInfo.phones) === null || _a === void 0 ? void 0 : _a.find((p) => p.phone_type === 'Main')) === null || _b === void 0 ? void 0 : _b.phone),
                mobilePhone: (0, elationMobilePhoneToE164_1.elationMobilePhoneToE164)((_d = (_c = patientInfo.phones) === null || _c === void 0 ? void 0 : _c.find((p) => p.phone_type === 'Mobile')) === null || _d === void 0 ? void 0 : _d.phone),
                email: (0, getLastEmail_1.getLastEmail)(patientInfo.emails),
                middleName: patientInfo.middle_name,
                actualName: patientInfo.actual_name,
                genderIdentity: patientInfo.gender_identity,
                legalGenderMarker: patientInfo.legal_gender_marker,
                pronouns: patientInfo.pronouns,
                sexualOrientation: patientInfo.sexual_orientation,
                ssn: patientInfo.ssn,
                ethnicity: patientInfo.ethnicity,
                race: patientInfo.race,
                preferredLanguage: patientInfo.preferred_language,
                notes: patientInfo.notes,
                previousFirstName: patientInfo.previous_first_name,
                previousLastName: patientInfo.previous_last_name,
                status: patientInfo.patient_status.status,
                preferredServiceLocationId: String(patientInfo.preferred_service_location),
                patientObject: JSON.stringify(patientInfo),
            },
        });
    },
};
//# sourceMappingURL=getPatient.js.map