"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createMetricEntry = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const extensions_core_2 = require("@awell-health/extensions-core");
const zod_1 = require("zod");
const zod_validation_error_1 = require("zod-validation-error");
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 settings_1 = require("../../settings");
const config_1 = require("./config");
exports.createMetricEntry = {
    key: 'createMetricEntry',
    category: extensions_core_2.Category.EHR_INTEGRATIONS,
    title: 'Create metric entry',
    description: 'Create a metric entry for a patient in Healthie.',
    fields: config_1.fields,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        try {
            const { settings, fields: { userId, category, metricStat }, } = (0, extensions_core_1.validate)({
                schema: zod_1.z.object({
                    settings: settings_1.SettingsValidationSchema,
                    fields: config_1.FieldsValidationSchema,
                }),
                payload,
            });
            const client = (0, graphqlClient_1.initialiseClient)(settings);
            if (client != null) {
                const sdk = (0, sdk_1.getSdk)(client);
                await sdk.createEntry({
                    metric_stat: String(metricStat),
                    user_id: userId,
                    type: 'MetricEntry',
                    category,
                });
                await onComplete({});
            }
        }
        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 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=createMetricEntry.js.map