"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.addHistory = void 0;
const zod_1 = require("zod");
const extensions_core_1 = require("@awell-health/extensions-core");
const settings_1 = require("../../settings");
const client_1 = require("../../client");
const config_1 = require("./config");
exports.addHistory = {
    key: 'addHistory',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Add History',
    description: "Add history item on Elation's patient page",
    fields: config_1.fields,
    previewable: true,
    onEvent: async ({ payload, onComplete, onError, helpers }) => {
        helpers.log({ fields: payload.fields }, 'Processing addHistory');
        try {
            const { fields, settings } = (0, extensions_core_1.validate)({
                schema: zod_1.z.object({
                    fields: config_1.FieldsValidationSchema,
                    settings: settings_1.SettingsValidationSchema,
                }),
                payload,
            });
            const api = (0, client_1.makeAPIClient)(settings);
            const addHistoryInput = {
                type: fields.type,
                patient: fields.patientId,
                text: fields.text,
            };
            helpers.log({ addHistoryInput }, '[addHistory] Adding Elation history');
            await api.addHistory(addHistoryInput);
            await onComplete();
        }
        catch (err) {
            helpers.log({ err }, 'error', err);
            const error = err;
            await onError({
                events: [
                    {
                        date: new Date().toISOString(),
                        text: { en: error.message },
                        error: {
                            category: 'SERVER_ERROR',
                            message: error.message,
                        },
                    },
                ],
            });
        }
    },
};
//# sourceMappingURL=addHistory.js.map