"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.listFormAnswers = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const zod_1 = require("zod");
const awell_1 = require("../../../../../src/lib/awell");
const generateFormOutput_1 = require("./lib/generateFormOutput");
exports.listFormAnswers = {
    key: 'listFormAnswers',
    category: extensions_core_1.Category.WORKFLOW,
    title: 'List form answers',
    description: 'List form answers',
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onEvent: async ({ payload, onComplete, onError, helpers }) => {
        const { fields: { language, includeDescriptions, includeMissingAnswers, separator, }, } = (0, extensions_core_1.validate)({
            schema: zod_1.z.object({
                fields: config_1.FieldsValidationSchema,
            }),
            payload,
        });
        const awellSdk = await helpers.awellSdk();
        const { formDefinition, formResponse } = await (0, awell_1.getLatestFormInCurrentStep)({
            awellSdk,
            pathwayId: payload.pathway.id,
            activityId: payload.activity.id,
        });
        const { result: output, omittedFormAnswers } = (0, generateFormOutput_1.generateFormOutput)({
            awellSdk,
            formDefinition,
            formResponse,
            language,
            includeDescriptions,
            includeMissingAnswers,
            separator,
        });
        await onComplete({
            data_points: {
                output,
            },
            events: [
                (0, awell_1.addActivityEventLog)({
                    message: `Not included in the output:\n ${omittedFormAnswers.map((answer) => `${answer.questionKey}: ${answer.reason}`).join('\n')}`,
                }),
            ],
        });
    },
};
//# sourceMappingURL=listFormAnswers.js.map