"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAppointment = void 0;
/* eslint-disable @typescript-eslint/naming-convention */
const zod_1 = require("zod");
const extensions_core_1 = require("@awell-health/extensions-core");
const extensions_core_2 = 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 appointment_zod_1 = require("../validation/appointment.zod");
const fields = {
    patientId: {
        id: 'patientId',
        label: 'Patient ID',
        description: 'The patient to book the appointment for',
        type: extensions_core_1.FieldType.NUMERIC,
        required: true,
    },
    scheduledDate: {
        id: 'scheduledDate',
        label: 'Scheduled date',
        description: 'Needs to be an ISO8601 string',
        type: extensions_core_1.FieldType.DATE,
        required: true,
    },
    reason: {
        id: 'reason',
        label: 'Reason',
        description: 'Should be one of the valid appointment types in Elation',
        type: extensions_core_1.FieldType.STRING,
        required: true,
    },
    physicianId: {
        id: 'physicianId',
        label: 'Physician ID',
        description: '',
        type: extensions_core_1.FieldType.NUMERIC,
        required: true,
    },
    practiceId: {
        id: 'practiceId',
        label: 'Practice ID',
        description: '',
        type: extensions_core_1.FieldType.NUMERIC,
        required: true,
    },
    duration: {
        id: 'duration',
        label: 'Duration',
        description: 'Number (in minutes). Must be a multiple of 5, the default duration is 15 minutes',
        type: extensions_core_1.FieldType.NUMERIC,
    },
    description: {
        id: 'description',
        label: 'Description',
        description: '',
        type: extensions_core_1.FieldType.STRING,
    },
    serviceLocationId: {
        id: 'serviceLocationId',
        label: 'Service location ID',
        description: '',
        type: extensions_core_1.FieldType.NUMERIC,
    },
    telehealthDetails: {
        id: 'telehealthDetails',
        label: 'Telehealth details',
        description: '',
        type: extensions_core_1.FieldType.STRING,
    },
};
const dataPoints = {
    appointmentId: {
        key: 'appointmentId',
        valueType: 'number',
    },
};
exports.createAppointment = {
    key: 'createAppointment',
    category: extensions_core_2.Category.EHR_INTEGRATIONS,
    title: 'Create Appointment',
    description: "Create an appointment using Elation's scheduling API.",
    fields,
    previewable: true,
    dataPoints,
    onActivityCreated: async (payload, onComplete, onError) => {
        var _a, _b;
        try {
            const { scheduledDate, patientId, physicianId, practiceId, serviceLocationId, telehealthDetails, ...fields } = payload.fields;
            const appointment = appointment_zod_1.appointmentSchema.parse({
                ...fields,
                scheduled_date: scheduledDate,
                patient: patientId,
                physician: physicianId,
                practice: practiceId,
                service_location: serviceLocationId,
                telehealth_details: telehealthDetails,
            });
            // API Call should produce AuthError or something dif.
            const api = (0, client_1.makeAPIClient)(payload.settings);
            const { id } = await api.createAppointment(appointment);
            await onComplete({
                data_points: {
                    appointmentId: String(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: 'SERVER_ERROR',
                                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: 'BAD_REQUEST',
                                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=createAppointment.js.map