"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.multiply = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const zod_1 = require("zod");
exports.multiply = {
    key: 'multiply',
    title: 'Multiply',
    description: 'Multiply a series of numbers to get a final product',
    category: extensions_core_1.Category.MATH,
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onEvent: async ({ payload, onComplete, onError, helpers }) => {
        helpers.log({ fields: payload.fields }, 'Processing multiply');
        try {
            const { fields } = (0, extensions_core_1.validate)({
                schema: zod_1.z.object({ fields: config_1.FieldsValidationSchema }),
                payload,
            });
            const multiplyAddends = (fields) => {
                return Object.values(fields).reduce((acc, curr) => acc * (curr !== null && curr !== void 0 ? curr : 1), 1);
            };
            const product = multiplyAddends(fields);
            helpers.log({ fields, product }, 'Calculated product');
            await onComplete({
                data_points: {
                    product: String(product),
                },
            });
        }
        catch (err) {
            helpers.log({ err }, 'error', err);
            const error = err;
            await onError({
                events: [
                    {
                        date: new Date().toISOString(),
                        text: { en: error.message },
                        error: {
                            category: 'SERVER_ERROR',
                            message: error.message,
                        },
                    },
                ],
            });
        }
    },
};
//# sourceMappingURL=multiply.js.map