"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.addVitals = void 0;
const zod_1 = require("zod");
const lodash_1 = require("lodash");
const extensions_core_1 = require("@awell-health/extensions-core");
const settings_1 = require("../../settings");
const client_1 = require("../../client");
const config_1 = require("./config");
// Helper function to create measurement objects with optional notes
const createMeasurement = (value, note) => {
    // the undefined check is stupid but otherwise the build is failing
    if (value !== undefined && !(0, lodash_1.isNil)(value)) {
        return (0, lodash_1.isEmpty)(note)
            ? [
                { value: value.toString() },
            ]
            : [
                { value: value.toString(), note },
            ];
    }
    return [];
};
exports.addVitals = {
    key: 'addVitals',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Add Vitals',
    description: 'Add vitals for the patient',
    fields: config_1.fields,
    previewable: true,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete }) => {
        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 body = {
            patient: fields.patientId,
            practice: fields.practiceId,
            visit_note: fields === null || fields === void 0 ? void 0 : fields.visitNoteId,
            non_visit_note: fields === null || fields === void 0 ? void 0 : fields.nonVisitNoteId,
            bmi: fields.bmi,
            height: createMeasurement(fields.height, fields.heightNote),
            weight: createMeasurement(fields.weight, fields.weightNote),
            oxygen: createMeasurement(fields.oxygen, fields.oxygenNote),
            rr: createMeasurement(fields.rr, fields.rrNote),
            hr: createMeasurement(fields.hr, fields.hrNote),
            hc: createMeasurement(fields.hc, fields.hcNote),
            temperature: createMeasurement(fields.temperature, fields.temperatureNote),
            bp: createMeasurement(fields.bp, fields.bpNote),
            bodyfat: createMeasurement(fields.bodyfat, fields.bodyfatNote),
            dlm: createMeasurement(fields.dlm, fields.dlmNote),
            bfm: createMeasurement(fields.bfm, fields.bfmNote),
            wc: createMeasurement(fields.wc, fields.wcNote),
        };
        const { id } = await api.addVitals(body);
        await onComplete({
            data_points: {
                vitalsId: String(id),
            },
        });
    },
};
//# sourceMappingURL=addVitals.js.map