"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAppointment = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const errors_1 = require("../../lib/sdk/graphql-codegen/errors");
const validatePayloadAndCreateSdk_1 = require("../../lib/sdk/validatePayloadAndCreateSdk");
const config_1 = require("./config");
const errors_2 = require("./lib/errors");
exports.createAppointment = {
    key: 'createAppointment',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Create 1:1 appointment',
    description: 'Create a 1:1 appointment in Healthie.',
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: false,
    onEvent: async ({ payload, onComplete, onError, helpers }) => {
        var _a, _b;
        const { fields, healthieSdk } = await (0, validatePayloadAndCreateSdk_1.validatePayloadAndCreateSdk)({
            fieldsSchema: config_1.FieldsValidationSchema,
            payload,
        });
        try {
            const res = await healthieSdk.client.mutation({
                createAppointment: {
                    __args: {
                        input: {
                            appointment_type_id: fields.appointmentTypeId,
                            contact_type: fields.contactTypeId,
                            other_party_id: fields.otherPartyId,
                            datetime: fields.datetime,
                            user_id: fields.patientId,
                            metadata: JSON.stringify(fields.metadata),
                            notes: fields.notes,
                            external_videochat_url: fields.externalVideochatUrl,
                        },
                    },
                    appointment: {
                        id: true,
                    },
                },
            });
            const appointmentId = (_b = (_a = res.createAppointment) === null || _a === void 0 ? void 0 : _a.appointment) === null || _b === void 0 ? void 0 : _b.id;
            if (appointmentId === undefined)
                throw new errors_2.HealthieAppointmentNotCreated(res);
            await onComplete({
                data_points: {
                    appointmentId,
                },
            });
        }
        catch (error) {
            if (error instanceof errors_2.HealthieAppointmentNotCreated) {
                await onError({
                    events: [(0, errors_2.parseHealthieAppointmentNotCreatedError)(error.errors)],
                });
            }
            else if (error instanceof errors_1.HealthieError) {
                const errors = (0, errors_1.mapHealthieToActivityError)(error.errors);
                await onError({
                    events: errors,
                });
            }
            else {
                // Handles Zod and other unknown errors
                throw error;
            }
        }
    },
};
//# sourceMappingURL=createAppointment.js.map