"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteNonVisitNote = 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 fields = {
    nonVisitNoteId: {
        id: 'nonVisitNoteId',
        label: 'Non-Visit Note ID',
        description: 'ID of the note to delete',
        type: extensions_core_1.FieldType.NUMERIC,
        required: true,
    },
};
exports.deleteNonVisitNote = {
    key: 'deleteNonVisitNote',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Delete Non-Visit Note',
    description: "Delete a Non-Visit Note using Elation's patient API.",
    fields,
    previewable: true,
    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);
            await api.deleteNonVisitNote(noteId);
            await onComplete();
        }
        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=deleteNonVisitNote.js.map