"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseNumberToTextWithDictionary = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const zod_validation_error_1 = require("zod-validation-error");
const zod_1 = require("zod");
exports.parseNumberToTextWithDictionary = {
    key: 'parseNumberToTextWithDictionary',
    title: 'Parse number to text with dictionary',
    description: 'Transform or parse a number to text based on a dictionary.',
    category: extensions_core_1.Category.DATA,
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        try {
            const { fields: { number, dictionary }, } = (0, extensions_core_1.validate)({
                schema: zod_1.z.object({
                    fields: config_1.FieldsValidationSchema,
                }),
                payload,
            });
            const getStringFromDictionary = () => {
                const key = number.toString();
                /**
                 * Get string value from the dict.
                 * If no mapping is found in dict, return the number as a string
                 */
                const output = Object.prototype.hasOwnProperty.call(dictionary, key)
                    ? dictionary[key]
                    : key;
                return String(output);
            };
            await onComplete({
                data_points: {
                    text: getStringFromDictionary(),
                },
            });
        }
        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.name },
                            error: {
                                category: 'WRONG_INPUT',
                                message: `${error.message}`,
                            },
                        },
                    ],
                });
                return;
            }
            const error = err;
            await onError({
                events: [
                    {
                        date: new Date().toISOString(),
                        text: { en: 'Something went wrong while orchestration the action' },
                        error: {
                            category: 'SERVER_ERROR',
                            message: error.message,
                        },
                    },
                ],
            });
        }
    },
};
//# sourceMappingURL=parseNumberToTextWithDictionary.js.map