"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get getDisplayTextAnswer () {
        return getDisplayTextAnswer;
    },
    get getResultName () {
        return getResultName;
    },
    get getSurveyAnswerRows () {
        return getSurveyAnswerRows;
    },
    get separateColorText () {
        return separateColorText;
    }
});
const _constants = require("@tamanu/constants");
const _fields = require("../fields");
const shouldShow = (component, components, valuesByCode)=>{
    switch(component.dataElement.type){
        case _constants.PROGRAM_DATA_ELEMENT_TYPES.DISPLAY_TEXT:
            // `checkVisibilityCriteria` could be called for all question types, but it’s redundant for
            // other question types. (A hidden question is always unanswered, which gets hidden anyway.)
            // DisplayText is a special case that’s expressly un-hidden, hence parsing visibilityCriteria.
            return (0, _fields.checkVisibilityCriteria)(component, components, valuesByCode);
        case _constants.PROGRAM_DATA_ELEMENT_TYPES.INSTRUCTION:
        case _constants.PROGRAM_DATA_ELEMENT_TYPES.SURVEY_LINK:
            return false;
        default:
            return true;
    }
};
const getDisplayTextAnswer = (component)=>{
    const { text, detail, dataElement } = component;
    return [
        text || dataElement.defaultText,
        detail
    ].filter(Boolean).join('\n') || undefined;
};
const getSurveyAnswerRows = ({ components, answers })=>{
    const componentsByDataElementId = new Map(components.map((c)=>[
            c.dataElement.id,
            c
        ]));
    const valuesByDataElementId = answers.reduce((acc, { dataElementId, body })=>{
        acc[dataElementId] = body;
        return acc;
    }, {});
    const valuesByCode = Object.entries(valuesByDataElementId).reduce((acc, [id, value])=>{
        const matchingComponent = componentsByDataElementId.get(id);
        if (matchingComponent) acc[matchingComponent.dataElement.code] = value;
        return acc;
    }, {});
    return components.map((component)=>{
        if (!shouldShow(component, components, valuesByCode)) return null;
        const { dataElement, id, screenIndex, config } = component;
        const { type: originalType, name } = dataElement;
        const answerObject = answers.find((a)=>a.dataElementId === dataElement.id);
        const answer = answerObject?.body ?? (originalType === _constants.PROGRAM_DATA_ELEMENT_TYPES.DISPLAY_TEXT ? getDisplayTextAnswer(component) : undefined);
        const sourceType = answerObject?.sourceType;
        const sourceConfig = answerObject?.sourceConfig;
        const componentConfig = originalType === _constants.PROGRAM_DATA_ELEMENT_TYPES.SURVEY_ANSWER ? sourceConfig : config;
        const type = originalType === _constants.PROGRAM_DATA_ELEMENT_TYPES.SURVEY_ANSWER ? sourceType : originalType;
        return {
            id,
            type,
            answer,
            name,
            screenIndex,
            dataElementId: dataElement.id,
            config: componentConfig,
            originalBody: answerObject?.originalBody,
            ...originalType === _constants.PROGRAM_DATA_ELEMENT_TYPES.DISPLAY_TEXT && {
                componentText: component.text,
                componentDetail: component.detail,
                defaultText: dataElement.defaultText
            }
        };
    }).filter((row)=>row != null);
};
const separateColorText = (resultText)=>{
    for (const [key, color] of Object.entries(_constants.RESULT_COLORS)){
        // only match colors at the end that follow a result
        // "90% GREEN" -> "90%"
        // "blue ribbon" -> "blue ribbon"
        // "reduced risk" -> "reduced risk"
        const re = RegExp(` ${key}$`, 'i');
        if (resultText.match(re)) {
            const strippedResultText = resultText.replace(re, '').trim();
            return {
                color,
                strippedResultText
            };
        }
    }
    return {
        strippedResultText: resultText
    };
};
const getResultName = (components)=>{
    const resultComponent = components.find((component)=>component.dataElement.type === 'Result');
    return resultComponent?.dataElement.defaultText;
};

//# sourceMappingURL=surveyAnswers.js.map