"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateNonVisitNote = void 0;
/* eslint-disable @typescript-eslint/naming-convention */
const extensions_core_1 = require("@awell-health/extensions-core");
const client_1 = require("../client");
const nonVisitNote_zod_1 = require("../validation/nonVisitNote.zod");
const lodash_1 = require("lodash");
/**
 * Although fields are not required, editing of e.g. bullet text requires providing a bullet id and an author
 */
const fields = {
    nonVisitNoteId: {
        id: 'nonVisitNoteId',
        label: 'Non-Visit Note ID',
        description: 'ID of a note.',
        type: extensions_core_1.FieldType.NUMERIC,
        required: true,
    },
    nonVisitNoteBulletId: {
        id: 'nonVisitNoteBulletId',
        label: 'Non-Visit Note Bullet ID',
        description: 'ID of a bullet in a note. Required when want to edit text, author or category of a note.',
        type: extensions_core_1.FieldType.NUMERIC,
        required: false,
    },
    text: {
        id: 'text',
        label: 'Text',
        description: 'Text of a note. Required when "Bullet ID" is provided.',
        type: extensions_core_1.FieldType.TEXT,
        required: false,
    },
    authorId: {
        id: 'authorId',
        label: 'Author',
        description: 'Author of a note. Should be ID of a User. Required when "Bullet ID" is provided.',
        type: extensions_core_1.FieldType.NUMERIC,
        required: false,
    },
    patientId: {
        id: 'patientId',
        label: 'Patient',
        description: 'ID of a Patient',
        type: extensions_core_1.FieldType.NUMERIC,
        required: false,
    },
    category: {
        id: 'category',
        label: 'Category',
        description: 'Category of a note. Defaults to "Problem". One from the list: "Problem", "Past", "Family", "Social", "Instr", "PE", "ROS", "Med", "Data", "Assessment", "Test", "Tx", "Narrative", "Followup", "Reason", "Plan", "Objective", "Hpi", "Allergies", "Habits", "Assessplan", "Consultant", "Attending", "Dateprocedure", "Surgical", "Orders", "Referenced", "Procedure".',
        type: extensions_core_1.FieldType.STRING,
        required: false,
    },
    practiceId: {
        id: 'practiceId',
        label: 'Practice',
        description: 'ID of a Practice.',
        type: extensions_core_1.FieldType.NUMERIC,
        required: false,
    },
    documentDate: {
        id: 'documentDate',
        label: 'Document Date',
        description: 'Date in ISO 8601 format.',
        type: extensions_core_1.FieldType.DATE,
        required: false,
    },
    chartDate: {
        id: 'chartDate',
        label: 'Chart Date',
        description: 'Date in ISO 8601 format.',
        type: extensions_core_1.FieldType.DATE,
        required: false,
    },
    tags: {
        id: 'tags',
        label: 'Tags',
        description: 'Comma-separated list of tags IDs.',
        type: extensions_core_1.FieldType.STRING,
        required: false,
    },
    signed_by: {
        id: 'signed_by',
        label: 'Signed by (user ID)',
        description: '⚠️ DEPRECATED - Please use the "Sign non-visit note" action to sign a non-visit note instead.',
        type: extensions_core_1.FieldType.NUMERIC,
        required: false,
    },
};
exports.updateNonVisitNote = {
    key: 'updateNonVisitNote',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Update Non-Visit Note',
    description: "Update a Non-Visit Note using Elation's patient API.",
    fields,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        const { nonVisitNoteId, nonVisitNoteBulletId, authorId, chartDate, documentDate, text, category, patientId, practiceId, signed_by, ...fields } = payload.fields;
        const noteId = extensions_core_1.NumericIdSchema.parse(nonVisitNoteId);
        // partial - all fields are optional
        const note = nonVisitNote_zod_1.nonVisitNoteSchema.partial().parse({
            ...fields,
            patient: patientId,
            practice: practiceId,
            bullets: (0, lodash_1.isNil)(nonVisitNoteBulletId)
                ? undefined
                : [{ id: nonVisitNoteBulletId, text, author: authorId, category }],
            document_date: documentDate,
            chart_date: chartDate,
            ...(!(0, lodash_1.isNil)(signed_by) && {
                signed_by,
                sign_date: new Date().toISOString(),
            }),
        });
        const api = (0, client_1.makeAPIClient)(payload.settings);
        await api.updateNonVisitNote(noteId, note);
        await onComplete();
    },
};
//# sourceMappingURL=updateNonVisitNote.js.map