"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.findPhysician = void 0;
const zod_1 = require("zod");
const extensions_core_1 = require("@awell-health/extensions-core");
const client_1 = require("../client");
const zod_validation_error_1 = require("zod-validation-error");
const axios_1 = require("axios");
const fields = {
    firstName: {
        id: 'firstName',
        label: 'First Name',
        description: 'First Name to search for.',
        type: extensions_core_1.FieldType.STRING,
        required: false,
    },
    lastName: {
        id: 'lastName',
        label: 'Last Name',
        description: 'Last Name to search for.',
        type: extensions_core_1.FieldType.STRING,
        required: false,
    },
    npi: {
        id: 'npi',
        label: 'NPI (National Provider Identifier)',
        description: 'NPI to search for.',
        type: extensions_core_1.FieldType.STRING,
        required: false,
    },
};
const dataPoints = {
    physicianId: {
        key: 'physicianId',
        valueType: 'number',
    },
    physicianFirstName: {
        key: 'physicianFirstName',
        valueType: 'string',
    },
    physicianLastName: {
        key: 'physicianLastName',
        valueType: 'string',
    },
    physicianCredentials: {
        key: 'physicianCredentials',
        valueType: 'string',
    },
    physicianEmail: {
        key: 'physicianEmail',
        valueType: 'string',
    },
    physicianNPI: {
        key: 'physicianNPI',
        valueType: 'string',
    },
    physicianUserId: {
        key: 'physicianUserId',
        valueType: 'number',
    },
    caregiverPracticeId: {
        key: 'caregiverPracticeId',
        valueType: 'number',
    },
};
exports.findPhysician = {
    key: 'findPhysician',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Find Physician',
    description: "Retrieve a physician using Elation's patient API.",
    fields,
    previewable: true,
    dataPoints,
    onActivityCreated: async (payload, onComplete, onError) => {
        var _a, _b;
        try {
            const { firstName, lastName, npi } = payload.fields;
            const api = (0, client_1.makeAPIClient)(payload.settings);
            const physiciansList = await api.findPhysicians({
                params: {
                    first_name: firstName,
                    last_name: lastName,
                    npi,
                },
            });
            if (physiciansList.count !== 1) {
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: {
                                en: `Find Physicians returned ${physiciansList.count} results, but the number of results must equal exactly 1.`,
                            },
                            error: {
                                category: 'WRONG_DATA',
                                message: `Find Physicians returned ${physiciansList.count} results, but the number of results must equal exactly 1.`,
                            },
                        },
                    ],
                });
                return;
            }
            const physician = physiciansList.results[0];
            await onComplete({
                data_points: {
                    physicianId: String(physician.id),
                },
            });
        }
        catch (err) {
            if (err instanceof zod_1.ZodError) {
                const error = (0, zod_validation_error_1.fromZodError)(err);
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: { en: error.message },
                            error: {
                                category: 'WRONG_INPUT',
                                message: error.message,
                            },
                        },
                    ],
                });
            }
            else if (err instanceof axios_1.AxiosError) {
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: {
                                en: `${(_a = err.status) !== null && _a !== void 0 ? _a : '(no status code)'} Error: ${err.message}`,
                            },
                            error: {
                                category: 'SERVER_ERROR',
                                message: `${(_b = err.status) !== null && _b !== void 0 ? _b : '(no status code)'} Error: ${err.message}`,
                            },
                        },
                    ],
                });
            }
            else {
                const message = err.message;
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: { en: message },
                            error: {
                                category: 'SERVER_ERROR',
                                message,
                            },
                        },
                    ],
                });
            }
        }
    },
};
//# sourceMappingURL=findPhysician.js.map