"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPhysician = 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 config_1 = require("./config");
exports.getPhysician = {
    key: 'getPhysician',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Get Physician',
    description: "Retrieve a physician using Elation's patient API.",
    fields: config_1.fields,
    previewable: true,
    dataPoints: config_1.dataPoints,
    onActivityCreated: async (payload, onComplete, onError) => {
        var _a, _b;
        try {
            const validatedFields = config_1.FieldsValidationSchema.parse(payload.fields);
            const api = (0, client_1.makeAPIClient)(payload.settings);
            const physician = await api.getPhysician(validatedFields.physicianId);
            await onComplete({
                data_points: {
                    physicianFirstName: physician.first_name,
                    physicianLastName: physician.last_name,
                    physicianCredentials: physician.credentials,
                    physicianEmail: physician.email,
                    physicianNPI: physician.npi,
                    physicianUserId: String(physician.user_id),
                    caregiverPracticeId: String(physician.practice),
                },
            });
        }
        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=getPhysician.js.map