"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDataPointValue = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const zod_1 = require("zod");
const addEventLog_1 = require("../../../../../src/lib/awell/addEventLog");
const awell_sdk_1 = require("@awell-health/awell-sdk");
exports.getDataPointValue = {
    key: 'getDataPointValue',
    category: extensions_core_1.Category.WORKFLOW,
    title: 'Get data point value',
    description: 'Get the value of a data point',
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onEvent: async ({ payload, onComplete, onError, helpers }) => {
        const { fields: { careFlowId, dataPointDefinitionId }, } = (0, extensions_core_1.validate)({
            schema: zod_1.z.object({
                fields: config_1.FieldsValidationSchema,
            }),
            payload,
        });
        const sdk = await helpers.awellSdk();
        const resp = await sdk.orchestration.query({
            pathwayDataPoints: {
                __args: {
                    pathway_id: careFlowId,
                    data_point_definition_id: dataPointDefinitionId,
                },
                success: true,
                dataPoints: {
                    id: true,
                    key: true,
                    serialized_value: true,
                    data_point_definition_id: true,
                    valueType: true,
                    date: true,
                },
            },
        });
        const dataPoints = resp.pathwayDataPoints.dataPoints;
        if (dataPoints.length === 0) {
            await onError({
                events: [
                    (0, addEventLog_1.addActivityEventLog)({
                        message: 'No data point values were found for the given data point definition id.',
                    }),
                ],
            });
            return;
        }
        /**
         * If a single data point definition has multiple values, there will be
         * multiple entries in the dataPoints array. We need to get the most recent
         */
        const mostRecentDataPoint = dataPoints.sort((a, b) => {
            return new Date(b.date).getTime() - new Date(a.date).getTime();
        })[0];
        await onComplete({
            data_points: {
                valueString: mostRecentDataPoint.valueType === awell_sdk_1.enumDataPointValueType.STRING
                    ? mostRecentDataPoint.serialized_value
                    : undefined,
                valueNumber: mostRecentDataPoint.valueType === awell_sdk_1.enumDataPointValueType.NUMBER
                    ? mostRecentDataPoint.serialized_value
                    : undefined,
                valueDate: mostRecentDataPoint.valueType === awell_sdk_1.enumDataPointValueType.DATE
                    ? mostRecentDataPoint.serialized_value
                    : undefined,
                valueJson: mostRecentDataPoint.valueType === awell_sdk_1.enumDataPointValueType.JSON
                    ? mostRecentDataPoint.serialized_value
                    : undefined,
                valueBoolean: mostRecentDataPoint.valueType === awell_sdk_1.enumDataPointValueType.BOOLEAN
                    ? mostRecentDataPoint.serialized_value
                    : undefined,
                valueTelephone: mostRecentDataPoint.valueType === awell_sdk_1.enumDataPointValueType.TELEPHONE
                    ? mostRecentDataPoint.serialized_value
                    : undefined,
                valueStringsArray: mostRecentDataPoint.valueType === awell_sdk_1.enumDataPointValueType.STRINGS_ARRAY
                    ? mostRecentDataPoint.serialized_value
                    : undefined,
                valueNumbersArray: mostRecentDataPoint.valueType === awell_sdk_1.enumDataPointValueType.NUMBERS_ARRAY
                    ? mostRecentDataPoint.serialized_value
                    : undefined,
            },
        });
    },
};
//# sourceMappingURL=getDataPointValue.js.map