"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createJournalEntry = void 0;
const lodash_1 = require("lodash");
const errors_1 = require("../../lib/sdk/graphql-codegen/errors");
const extensions_core_1 = require("@awell-health/extensions-core");
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.createJournalEntry = {
    key: 'createJournalEntry',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Create journal entry',
    description: 'Create a journal entry in Healthie.',
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        var _a, _b;
        const { fields, settings } = payload;
        const { id, type, percieved_hungriness } = fields;
        try {
            if ((0, lodash_1.isNil)(id) || (0, lodash_1.isNil)(type)) {
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: { en: 'Fields are missing' },
                            error: {
                                category: 'MISSING_FIELDS',
                                message: '`id` or `type` is missing',
                            },
                        },
                    ],
                });
                return;
            }
            const client = (0, graphqlClient_1.initialiseClient)(settings);
            if (client !== undefined) {
                const sdk = (0, sdk_1.getSdk)(client);
                const { data } = await sdk.createJournalEntry({
                    input: {
                        user_id: id,
                        type,
                        percieved_hungriness: String(percieved_hungriness),
                    },
                });
                const journalEntryId = (_b = (_a = data.createEntry) === null || _a === void 0 ? void 0 : _a.entry) === null || _b === void 0 ? void 0 : _b.id;
                await onComplete({
                    data_points: {
                        journalEntryId,
                    },
                });
            }
            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=createJournalEntry.js.map