"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNonVisitNote = void 0;
/* eslint-disable @typescript-eslint/naming-convention */
const zod_1 = require("zod");
const extensions_core_1 = require("@awell-health/extensions-core");
const client_1 = require("../client");
const zod_validation_error_1 = require("zod-validation-error");
const axios_1 = require("axios");
const lodash_1 = require("lodash");
const fields = {
    nonVisitNoteId: {
        id: 'nonVisitNoteId',
        label: 'Non-Visit Note ID',
        description: 'ID of a note',
        type: extensions_core_1.FieldType.NUMERIC,
        required: true,
    },
};
const dataPoints = {
    text: {
        key: 'text',
        valueType: 'string',
    },
    authorId: {
        key: 'authorId',
        valueType: 'number',
    },
    patientId: {
        key: 'patientId',
        valueType: 'number',
    },
    practiceId: {
        key: 'practiceId',
        valueType: 'number',
    },
    documentDate: {
        key: 'documentDate',
        valueType: 'date',
    },
    chartDate: {
        key: 'chartDate',
        valueType: 'date',
    },
    tags: {
        key: 'tags',
        valueType: 'string',
    },
};
exports.getNonVisitNote = {
    key: 'getNonVisitNote',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Get Non-Visit Note',
    description: "Get a Non-Visit Note using Elation's patient API.",
    fields,
    previewable: true,
    dataPoints,
    onActivityCreated: async (payload, onComplete, onError) => {
        var _a, _b;
        try {
            const { nonVisitNoteId } = payload.fields;
            const noteId = extensions_core_1.NumericIdSchema.parse(nonVisitNoteId);
            const api = (0, client_1.makeAPIClient)(payload.settings);
            const { bullets, chart_date, document_date, patient, practice, tags } = await api.getNonVisitNote(noteId);
            await onComplete({
                data_points: {
                    authorId: (bullets === null || bullets === void 0 ? void 0 : bullets.length) !== 0 ? String(bullets[0].author) : undefined,
                    text: (bullets === null || bullets === void 0 ? void 0 : bullets.length) !== 0 ? bullets[0].text : undefined,
                    chartDate: chart_date,
                    documentDate: document_date,
                    patientId: String(patient),
                    practiceId: !(0, lodash_1.isNil)(practice) ? String(practice) : undefined,
                    tags: (tags === null || tags === void 0 ? void 0 : tags.length) !== 0
                        ? tags === null || tags === void 0 ? void 0 : tags.map((tag) => tag.id).join(',')
                        : undefined,
                },
            });
        }
        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 axios_1.AxiosError) {
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: {
                                en: `${(_a = err.status) !== null && _a !== void 0 ? _a : '(no status code)'} Error: ${err.message}`,
                            },
                            error: {
                                category: 'SERVER_ERROR',
                                message: `${(_b = err.status) !== null && _b !== void 0 ? _b : '(no status code)'} Error: ${err.message}`,
                            },
                        },
                    ],
                });
            }
            else {
                const message = err.message;
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: { en: message },
                            error: {
                                category: 'SERVER_ERROR',
                                message,
                            },
                        },
                    ],
                });
            }
        }
    },
};
//# sourceMappingURL=getNonVisitNote.js.map