"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getResponsesForAllForms = exports.getFormResponseText = exports.getFormTitle = void 0;
const getBooleanAnswer = (answer) => {
    if (answer === '0')
        return 'Answer: No';
    if (answer === '1')
        return 'Answer: Yes';
    return 'Answer: unknown';
};
const getSingleSelectAnswer = (questionDefinition, questionResponse) => {
    var _a, _b;
    const answerValue = questionResponse.value;
    const answerOptions = (_a = questionDefinition.options) !== null && _a !== void 0 ? _a : [];
    const allAnswerOptions = answerOptions === null || answerOptions === void 0 ? void 0 : answerOptions.map((option) => {
        return `- ${option.label} (${option.value_string})`;
    }).join('\n');
    const answerLabel = (_b = answerOptions === null || answerOptions === void 0 ? void 0 : answerOptions.find((answerOption) => answerOption.value_string === answerValue)) === null || _b === void 0 ? void 0 : _b.label;
    return `Possible answers:\n${allAnswerOptions}\nAnswer: ${String(answerLabel)}`;
};
const getMultipleSelectAnswers = (questionDefinition, questionResponse) => {
    var _a;
    const answerValues = JSON.parse(questionResponse.value);
    const answerOptions = (_a = questionDefinition.options) !== null && _a !== void 0 ? _a : [];
    const answerLabels = answerValues
        .map((answerValue) => {
        var _a;
        return (_a = answerOptions === null || answerOptions === void 0 ? void 0 : answerOptions.find((answerOption) => answerOption.value_string === String(answerValue))) === null || _a === void 0 ? void 0 : _a.label;
    })
        .filter((answer) => answer !== undefined);
    const allAnswerOptions = answerOptions === null || answerOptions === void 0 ? void 0 : answerOptions.map((option) => {
        return `- ${option.label} (${option.value_string})`;
    }).join('\n');
    return `Possible answers:\n${allAnswerOptions}\nAnswers:\n${answerLabels
        .map((_answerLabel) => `- ${_answerLabel}`)
        .join('\n')}`;
};
const getQuestionAndAnswer = (questionDefinition, questionResponse) => {
    const userQuestionType = questionDefinition.userQuestionType;
    const addQuestionLabel = (answer) => {
        var _a;
        return `Question: ${questionDefinition.title}\nQuestion type: ${(_a = questionDefinition.userQuestionType) !== null && _a !== void 0 ? _a : 'UNKNOWN'}\n${answer}`;
    };
    switch (userQuestionType) {
        case 'YES_NO':
            return addQuestionLabel(getBooleanAnswer(questionResponse.value));
        case 'DATE':
            return addQuestionLabel(`Answer: ${String(questionResponse.value)}`);
        case 'NUMBER':
            return addQuestionLabel(`Answer: ${String(questionResponse.value)}`);
        case 'LONG_TEXT':
            return addQuestionLabel(`Answer: ${String(questionResponse.value)}`);
        case 'MULTIPLE_CHOICE':
            return addQuestionLabel(getSingleSelectAnswer(questionDefinition, questionResponse));
        case 'MULTIPLE_SELECT':
            return addQuestionLabel(getMultipleSelectAnswers(questionDefinition, questionResponse));
        case 'SLIDER':
            return addQuestionLabel(`Answer: ${String(questionResponse.value)}`);
        case 'SHORT_TEXT':
            return addQuestionLabel(`Answer: ${String(questionResponse.value)}`);
        case 'TELEPHONE':
            return addQuestionLabel(`Answer: ${String(questionResponse.value)}`);
        default:
            return addQuestionLabel(`Answer: ${String(questionResponse.value)}`);
    }
};
const getFormTitle = (opts) => {
    return opts.formDefinition.title;
};
exports.getFormTitle = getFormTitle;
const getFormResponseText = (opts) => {
    const formAnswers = [];
    const omittedFormAnswers = [];
    opts.formDefinition.questions.forEach((questionDefinition) => {
        const questionResponse = opts.formResponse.answers.find((a) => a.question_id === questionDefinition.id);
        if (questionResponse === undefined) {
            omittedFormAnswers.push({
                questionId: questionDefinition.id,
                reason: 'No corresponding answer found in the form response',
            });
            return;
        }
        try {
            const answer = getQuestionAndAnswer(questionDefinition, questionResponse);
            formAnswers.push(answer);
        }
        catch (error) {
            const errorMessage = error instanceof Error ? error.message : 'Unknown error';
            omittedFormAnswers.push({
                questionId: questionDefinition.id,
                reason: `Error processing answer: ${errorMessage}`,
            });
        }
    });
    const formTitle = (0, exports.getFormTitle)({ formDefinition: opts.formDefinition });
    return {
        result: `Form Title: ${formTitle}\n\n${formAnswers.join('\n\n----------------\n\n')}`,
        omittedFormAnswers,
    };
};
exports.getFormResponseText = getFormResponseText;
const getResponsesForAllForms = (opts) => {
    const allFormResponses = opts.formsData.map((formData) => {
        const formResponseText = (0, exports.getFormResponseText)({
            formDefinition: formData.formDefinition,
            formResponse: formData.formResponse,
        });
        return {
            formActivityId: formData.formActivityId,
            formId: formData.formId,
            ...formResponseText
        };
    });
    const concatenatedResponses = allFormResponses
        .map((response) => `Form Activity ID: ${response.formActivityId}\nForm ID: ${response.formId}\n${response.result}`)
        .join('\n\n==========Next Form==========\n\n');
    const allOmittedAnswers = allFormResponses.flatMap((response) => response.omittedFormAnswers);
    return {
        result: concatenatedResponses,
        omittedFormAnswers: allOmittedAnswers,
    };
};
exports.getResponsesForAllForms = getResponsesForAllForms;
//# sourceMappingURL=getFormResponseText.js.map