"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMetricEntry = void 0;
const zod_1 = require("zod");
const zod_validation_error_1 = require("zod-validation-error");
const extensions_core_1 = require("@awell-health/extensions-core");
const extensions_core_2 = require("@awell-health/extensions-core");
const settings_1 = require("../../settings");
const errors_1 = require("../../lib/sdk/graphql-codegen/errors");
const config_1 = require("./config");
const graphqlClient_1 = require("../../lib/sdk/graphql-codegen/graphqlClient");
const sdk_1 = require("../../lib/sdk/graphql-codegen/generated/sdk");
const lodash_1 = require("lodash");
exports.getMetricEntry = {
    key: 'getMetricEntry',
    category: extensions_core_2.Category.EHR_INTEGRATIONS,
    title: 'Get metric entry',
    description: 'Get most recent metric entry of a given category from Healthie.',
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        var _a, _b, _c;
        try {
            const { settings, fields: { patientId, category }, } = (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);
                const { data } = await sdk.entries({
                    type: 'MetricEntry',
                    category,
                    client_id: patientId,
                });
                const mostRecentMetricObject = data.entries === undefined || ((_a = data.entries) === null || _a === void 0 ? void 0 : _a.length) === 0
                    ? undefined
                    : (_b = data.entries) === null || _b === void 0 ? void 0 : _b[0];
                await onComplete({
                    data_points: {
                        metricId: mostRecentMetricObject === null || mostRecentMetricObject === void 0 ? void 0 : mostRecentMetricObject.id,
                        metricValue: (0, lodash_1.isEmpty)(mostRecentMetricObject)
                            ? undefined
                            : String(mostRecentMetricObject === null || mostRecentMetricObject === void 0 ? void 0 : mostRecentMetricObject.metric_stat),
                        createdAt: (0, lodash_1.isEmpty)(mostRecentMetricObject)
                            ? undefined
                            : new Date((_c = mostRecentMetricObject === null || mostRecentMetricObject === void 0 ? void 0 : mostRecentMetricObject.created_at) !== null && _c !== void 0 ? _c : '').toISOString(),
                    },
                });
            }
        }
        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=getMetricEntry.js.map