"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPatient = void 0;
const lodash_1 = require("lodash");
const extensions_core_1 = require("@awell-health/extensions-core");
const errors_1 = require("../../lib/sdk/graphql-codegen/errors");
const config_1 = require("./config");
const validatePayloadAndCreateSdk_1 = require("../../lib/sdk/validatePayloadAndCreateSdk");
const errors_2 = require("./lib/errors");
exports.createPatient = {
    key: 'createPatient',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Create a patient',
    description: 'Create a patient in Healthie.',
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onEvent: async ({ payload, onComplete, onError, helpers }) => {
        var _a, _b, _c, _d;
        const { fields, healthieSdk } = await (0, validatePayloadAndCreateSdk_1.validatePayloadAndCreateSdk)({
            fieldsSchema: config_1.FieldsValidationSchema,
            payload,
        });
        const dont_send_welcome = fields.send_invite !== true;
        const dietitian_id = fields.provider_id === undefined || fields.provider_id === ''
            ? undefined
            : fields.provider_id;
        const skipped_email = fields.email === undefined || fields.email === ''; // if email is empty we still want to create the patient
        try {
            const input = {
                first_name: fields.first_name,
                last_name: fields.last_name,
                legal_name: fields.legal_name,
                email: fields.email,
                phone_number: fields.phone_number,
                dob: fields.dob,
                dietitian_id,
                dont_send_welcome,
            };
            if (skipped_email) {
                input.skipped_email = true;
            }
            const res = await healthieSdk.client.mutation({
                createClient: {
                    __args: {
                        input,
                    },
                    user: {
                        id: true,
                        set_password_link: true, // for testing
                    },
                    messages: {
                        __scalar: true,
                    },
                },
            });
            if ((0, lodash_1.isEmpty)((_b = (_a = res === null || res === void 0 ? void 0 : res.createClient) === null || _a === void 0 ? void 0 : _a.user) === null || _b === void 0 ? void 0 : _b.id))
                throw new errors_2.HealthiePatientNotCreated(res);
            await onComplete({
                data_points: {
                    healthiePatientId: (_d = (_c = res.createClient) === null || _c === void 0 ? void 0 : _c.user) === null || _d === void 0 ? void 0 : _d.id,
                },
            });
        }
        catch (error) {
            if (error instanceof errors_2.HealthiePatientNotCreated) {
                await onError({
                    events: [(0, errors_2.parseHealthiePatientNotCreatedError)(error.errors)],
                });
            }
            else if (error instanceof errors_1.HealthieError) {
                const errors = (0, errors_1.mapHealthieToActivityError)(error.errors);
                await onError({
                    events: errors,
                });
            }
            else {
                throw error;
            }
        }
    },
};
//# sourceMappingURL=createPatient.js.map