"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFormAnswers = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const validatePayloadAndCreateSdk_1 = require("../../lib/sdk/validatePayloadAndCreateSdk");
const processFormAnswers_1 = require("../../lib/helpers/processFormAnswers");
exports.getFormAnswers = {
    key: 'getFormAnswers',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Get form answers',
    description: 'Retrieve form answers from form answer group',
    fields: config_1.fields,
    previewable: true,
    dataPoints: config_1.dataPoints,
    onActivityCreated: async (payload, onComplete, onError) => {
        var _a;
        const { fields: input, sdk, settings, } = await (0, validatePayloadAndCreateSdk_1.validatePayloadAndCreateSdk)({
            fieldsSchema: config_1.FieldsValidationSchema,
            payload,
        });
        const { formAnswerMaxSizeKB } = settings;
        try {
            const res = await sdk.getFormAnswerGroup(input);
            const rawFormAnswerGroup = (_a = res === null || res === void 0 ? void 0 : res.data) === null || _a === void 0 ? void 0 : _a.formAnswerGroup;
            if (!rawFormAnswerGroup) {
                throw new Error(`Form answer group not found for id: ${input.id}`);
            }
            // Process form answers to replace large ones with error messages
            const processedFormAnswerGroup = (0, processFormAnswers_1.processFormAnswersForSize)(rawFormAnswerGroup, formAnswerMaxSizeKB);
            await onComplete({
                data_points: {
                    formAnswers: JSON.stringify(processedFormAnswerGroup.form_answers),
                },
            });
        }
        catch (error) {
            await onError({
                events: [
                    {
                        date: new Date().toISOString(),
                        text: {
                            en: error instanceof Error ? error.message : 'Unknown error',
                        },
                        error: {
                            category: 'BAD_REQUEST',
                            message: error instanceof Error ? error.message : 'Unknown error',
                        },
                    },
                ],
            });
        }
    },
};
//# sourceMappingURL=getFormAnswers.js.map