"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertToMetriportPatient = exports.createPatient = void 0;
const api_sdk_1 = require("@metriport/api-sdk");
const driver_license_validator_1 = require("driver-license-validator");
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 validation_1 = require("./validation");
const dataPoints_1 = require("./dataPoints");
exports.createPatient = {
    key: 'createPatient',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Create Patient',
    description: 'Creates a Patient in Metriport for the specified Facility where the Patient is receiving care.',
    fields: fields_1.createFields,
    previewable: true,
    dataPoints: dataPoints_1.patientIdDataPoint,
    onActivityCreated: async (payload, onComplete, onError) => {
        try {
            const patient = validation_1.patientCreateSchema.parse(payload.fields);
            const facilityId = generic_zod_1.stringId.parse(payload.fields.facilityId);
            const patientMetriport = (0, exports.convertToMetriportPatient)(patient);
            const api = (0, client_1.createMetriportApi)(payload.settings);
            const { id } = await api.createPatient(patientMetriport, facilityId);
            await onComplete({
                data_points: {
                    patientId: String(id),
                },
            });
        }
        catch (err) {
            await (0, errorHandler_1.handleErrorMessage)(err, onError);
        }
    },
};
const convertToMetriportPatient = (patient) => {
    var _a;
    const patientMetriport = {
        firstName: patient.firstName,
        lastName: patient.lastName,
        dob: patient.dob,
        genderAtBirth: patient.genderAtBirth,
        address: {
            addressLine1: patient.addressLine1,
            addressLine2: patient.addressLine2,
            city: patient.city,
            state: patient.state,
            zip: patient.zip,
            country: patient.country,
        },
        personalIdentifiers: [],
        contact: {
            phone: patient.phone,
            email: patient.email,
        },
    };
    if (patient.driversLicenseState !== undefined &&
        patient.driversLicenseValue === undefined) {
        throw new Error('Drivers license value is required when drivers license state is provided');
    }
    else if (patient.driversLicenseState === undefined &&
        patient.driversLicenseValue !== undefined) {
        throw new Error('Drivers license state is required when drivers license value is provided');
    }
    if (patient.driversLicenseState !== undefined &&
        patient.driversLicenseState.length > 0 &&
        patient.driversLicenseValue !== undefined &&
        patient.driversLicenseValue.length > 0) {
        const valid = (0, driver_license_validator_1.isValid)(patient.driversLicenseValue, {
            states: patient.driversLicenseState,
        });
        if (valid) {
            ;
            ((_a = patientMetriport.personalIdentifiers) !== null && _a !== void 0 ? _a : []).push({
                type: 'driversLicense',
                value: patient.driversLicenseValue,
                state: api_sdk_1.usStateSchema.parse(patient.driversLicenseState),
            });
        }
    }
    return patientMetriport;
};
exports.convertToMetriportPatient = convertToMetriportPatient;
//# sourceMappingURL=create.js.map