"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateFormOutput = void 0;
const lodash_1 = require("lodash");
const BOOLEAN_TRANSLATIONS = {
    '0': {
        Dutch: 'Nee',
        French: 'Non',
        English: 'No',
    },
    '1': {
        Dutch: 'Ja',
        French: 'Oui',
        English: 'Yes',
    },
};
const MISSING_ANSWER_TRANSLATIONS = {
    Dutch: 'Geen antwoord of niet van toepassing',
    French: 'Aucune réponse ou non applicable',
    English: 'No answer or not applicable',
};
const getBooleanAnswer = (answer, language) => {
    var _a, _b;
    return (_b = (_a = BOOLEAN_TRANSLATIONS[answer]) === null || _a === void 0 ? void 0 : _a[language]) !== null && _b !== void 0 ? _b : 'Unknown';
};
const getMissingAnswer = (questionDefinition, language) => {
    const missingAnswerTranslation = MISSING_ANSWER_TRANSLATIONS[language];
    return `${questionDefinition.title}:\n${missingAnswerTranslation}`;
};
const getSingleSelectAnswer = (questionDefinition, questionResponse) => {
    var _a, _b;
    const answerValue = questionResponse.value;
    const answerOptions = (_a = questionDefinition.options) !== null && _a !== void 0 ? _a : [];
    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 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);
    return answerLabels.map((_answerLabel) => `- ${_answerLabel}`).join('\n');
};
const languageToLocaleString = (language) => {
    switch (language) {
        case 'Dutch':
            return 'nl-NL';
        case 'French':
            return 'fr-FR';
        case 'English':
            return 'en-US';
        default:
            return 'en-US';
    }
};
const getDateAnswer = (answer, language) => {
    const date = new Date(answer);
    const locale = languageToLocaleString(language);
    return date.toLocaleDateString(locale);
};
const getQuestionAndAnswer = (questionDefinition, questionResponse, language) => {
    const userQuestionType = questionDefinition.userQuestionType;
    const addQuestionLabel = (answer) => {
        return `${questionDefinition.title}:\n${answer}`;
    };
    switch (userQuestionType) {
        case 'YES_NO':
            return addQuestionLabel(getBooleanAnswer(questionResponse.value, language));
        case 'DATE':
            return addQuestionLabel(getDateAnswer(questionResponse.value, language));
        case 'NUMBER':
            return addQuestionLabel(String(questionResponse.value));
        case 'LONG_TEXT':
            return addQuestionLabel(String(questionResponse.value));
        case 'MULTIPLE_CHOICE':
            return addQuestionLabel(getSingleSelectAnswer(questionDefinition, questionResponse));
        case 'MULTIPLE_SELECT':
            return addQuestionLabel(getMultipleSelectAnswers(questionDefinition, questionResponse));
        case 'SLIDER':
            return addQuestionLabel(String(questionResponse.value));
        case 'SHORT_TEXT':
            return addQuestionLabel(String(questionResponse.value));
        case 'TELEPHONE':
            return addQuestionLabel(String(questionResponse.value));
        default:
            return addQuestionLabel(String(questionResponse.value));
    }
};
const generateFormOutput = (opts) => {
    const formAnswers = [];
    const omittedFormAnswers = [];
    opts.formDefinition.questions.forEach((questionDefinition) => {
        var _a, _b;
        const questionResponse = opts.formResponse.answers.find((a) => a.question_id === questionDefinition.id);
        if (questionResponse === undefined) {
            if (questionDefinition.userQuestionType === 'DESCRIPTION' &&
                opts.includeDescriptions) {
                const escapedDescription = opts.awellSdk.utils.awell.slateToEscapedJsString((_b = (_a = questionDefinition.title_html) !== null && _a !== void 0 ? _a : questionDefinition.title) !== null && _b !== void 0 ? _b : '');
                formAnswers.push(escapedDescription);
                return;
            }
            if (opts.includeMissingAnswers) {
                formAnswers.push(getMissingAnswer(questionDefinition, opts.language));
                return;
            }
            omittedFormAnswers.push({
                questionId: questionDefinition.id,
                questionKey: questionDefinition.key,
                reason: questionDefinition.userQuestionType === 'DESCRIPTION'
                    ? 'Question is a description and is not included in the output'
                    : 'Answer is missing in the form response and is not included in the output',
            });
            return;
        }
        try {
            const answer = getQuestionAndAnswer(questionDefinition, questionResponse, opts.language);
            formAnswers.push(answer);
        }
        catch (error) {
            const errorMessage = error instanceof Error ? error.message : 'Unknown error';
            omittedFormAnswers.push({
                questionId: questionDefinition.id,
                questionKey: questionDefinition.key,
                reason: `Error processing answer: ${errorMessage}`,
            });
        }
    });
    const separator = !(0, lodash_1.isNil)(opts.separator) && !(0, lodash_1.isEmpty)(opts.separator)
        ? `\n\n${opts.separator}\n\n`
        : '\n\n';
    return {
        result: formAnswers.join(separator),
        omittedFormAnswers,
    };
};
exports.generateFormOutput = generateFormOutput;
//# sourceMappingURL=generateFormOutput.js.map