"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkScheduledAppointments = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const errors_1 = require("../../lib/sdk/graphql-codegen/errors");
const sdk_1 = require("../../lib/sdk/graphql-codegen/generated/sdk");
const graphqlClient_1 = require("../../lib/sdk/graphql-codegen/graphqlClient");
const config_1 = require("./config");
exports.checkScheduledAppointments = {
    key: 'checkScheduledAppointments',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Check scheduled appointments',
    description: 'Check if the given patient has an active appointment scheduled for the given appointment type.',
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        var _a;
        const { fields, settings } = payload;
        const { patientId, appointmentTypeId } = fields;
        try {
            const client = (0, graphqlClient_1.initialiseClient)(settings);
            if (client !== undefined) {
                const sdk = (0, sdk_1.getSdk)(client);
                const { data } = await sdk.getScheduledAppointments({
                    user_id: patientId,
                    appointment_type_id: appointmentTypeId,
                });
                const appointments = (_a = data.appointments) !== null && _a !== void 0 ? _a : [];
                const isScheduled = appointments.length > 0;
                await onComplete({
                    data_points: {
                        isScheduled: String(isScheduled),
                    },
                });
            }
            else {
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: { en: 'API client requires an API url and API key' },
                            error: {
                                category: 'MISSING_SETTINGS',
                                message: 'Missing api url or api key',
                            },
                        },
                    ],
                });
            }
        }
        catch (err) {
            if (err instanceof errors_1.HealthieError) {
                const errors = (0, errors_1.mapHealthieToActivityError)(err.errors);
                await onError({
                    events: errors,
                });
            }
            else {
                const error = err;
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: { en: 'Healthie API reported an error' },
                            error: {
                                category: 'SERVER_ERROR',
                                message: error.message,
                            },
                        },
                    ],
                });
            }
        }
    },
};
//# sourceMappingURL=checkScheduledAppointments.js.map