import * as _ from 'lodash-es';
import React, { useContext } from 'react';
import { getNumericAttribute } from '../../../dom-utils';
import { shortDisplayNumber } from '../../../shortDisplayNumber';
import { mapMaybe } from '../../../utils';
import { QuestionContext } from '../../context/QuestionContext';
import { findScore, ResultsContext } from '../../context/ResultsContext';
import { AnnotationLists } from '../../shared/AnnotationLists';
const hasAnnotations = (score) => { var _a, _b, _c, _d; return Boolean(((_b = (_a = score === null || score === void 0 ? void 0 : score.pregrading) === null || _a === void 0 ? void 0 : _a.annotations) === null || _b === void 0 ? void 0 : _b.length) || ((_d = (_c = score === null || score === void 0 ? void 0 : score.censoring) === null || _c === void 0 ? void 0 : _c.annotations) === null || _d === void 0 ? void 0 : _d.length)); };
const getPrefix = (answers, answer) => answers.length > 1 ? shortDisplayNumber(answer.getAttribute('display-number')) : '';
function ResultsAnnotationList() {
    const { answers } = useContext(QuestionContext);
    const { scores, singleGrading } = useContext(ResultsContext);
    const answersAndScores = mapMaybe(answers || [], answer => {
        const questionId = getNumericAttribute(answer, 'question-id');
        const score = findScore(scores, questionId);
        return score && answer && hasAnnotations(score) ? [answer, score] : undefined;
    });
    const getListOfAnnotations = (answerElementAndScores, annotationsFrom, listNumberOffset = 0) => _.flatMap(answerElementAndScores, ([answer, score]) => {
        var _a, _b, _c;
        return (_c = (_b = (_a = score[annotationsFrom]) === null || _a === void 0 ? void 0 : _a.annotations) === null || _b === void 0 ? void 0 : _b.filter(a => a.message.length).map((annotation, i) => {
            const numbering = `${getPrefix(answers, answer) + String(listNumberOffset + i + 1)})`;
            const message = annotation.message;
            return { numbering, message };
        })) !== null && _c !== void 0 ? _c : [];
    });
    const pregradingAnnotations = getListOfAnnotations(answersAndScores, 'pregrading');
    const censoringAnnotations = getListOfAnnotations(answersAndScores, 'censoring', pregradingAnnotations.length);
    return (React.createElement(AnnotationLists, { pregradingAnnotations: pregradingAnnotations, censoringAnnotations: censoringAnnotations, singleGrading: !!singleGrading }));
}
export default React.memo(ResultsAnnotationList);
//# sourceMappingURL=AnnotationList.js.map