"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPatient = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const utils_1 = require("../../utils");
const lodash_1 = require("lodash");
exports.createPatient = {
    key: 'createPatient',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Create patient',
    description: 'Create a patient in Medplum',
    fields: config_1.fields,
    previewable: false,
    dataPoints: config_1.dataPoints,
    onActivityCreated: async (payload, onComplete, onError) => {
        var _a;
        const { fields: input, medplumSdk, patient, } = await (0, utils_1.validateAndCreateSdkClient)({
            fieldsSchema: config_1.FieldsValidationSchema,
            payload,
        });
        const mobilePhone = !(0, lodash_1.isEmpty)(input.mobilePhone)
            ? {
                system: 'phone',
                value: input.mobilePhone,
                use: 'mobile',
            }
            : null;
        const email = !(0, lodash_1.isEmpty)(input.email)
            ? {
                system: 'email',
                value: input.email,
                use: 'home',
            }
            : null;
        const addressLine = !(0, lodash_1.isEmpty)(input.address) ? [(_a = input.address) !== null && _a !== void 0 ? _a : ''] : [];
        /**
         * We only create the patient in Medplum if that patient
         * doesn't exist yet.
         */
        const res = await medplumSdk.createResourceIfNoneExist({
            resourceType: 'Patient',
            identifier: [
                {
                    system: 'https://www.awellhealth.com/',
                    value: patient.id,
                    assigner: {
                        display: 'Awell',
                    },
                },
            ],
            name: [
                {
                    use: 'official',
                    family: input.lastName,
                    given: (0, lodash_1.isEmpty)(input.firstName) ? [] : [String(input.firstName)],
                },
            ],
            telecom: [
                ...((mobilePhone != null) ? [mobilePhone] : []),
                ...((email != null) ? [email] : []),
            ],
            birthDate: input.birthDate,
            gender: input.gender,
            address: [
                {
                    use: 'home',
                    line: addressLine,
                    city: input.city,
                    state: input.state,
                    postalCode: input.postalCode,
                    country: input.country,
                },
            ],
        }, `identifier=${utils_1.AWELL_IDENTIFIER_SYSTEM}|${patient.id}`);
        await onComplete({
            data_points: {
                // @ts-expect-error id is not included in the response type?
                patientId: res.id,
            },
        });
    },
};
//# sourceMappingURL=createPatient.js.map