import classNames from 'classnames';
import React, { useContext } from 'react';
import { findChildrenAnswers, getNumericAttribute } from '../../dom-utils';
import { QuestionContext, withQuestionContext } from '../context/QuestionContext';
import AnnotationList from './internal/AnnotationList';
import { ResultsContext } from '../context/ResultsContext';
import { useExamTranslation } from '../../i18n';
function Question({ element, renderChildNodes }) {
    var _a, _b;
    const { answersByQuestionId, gradingStructure } = useContext(ResultsContext);
    const { displayNumber, level } = useContext(QuestionContext);
    const hasAnswers = questionHasAnswers(element, answersByQuestionId);
    const { i18n } = useExamTranslation();
    const question = (_a = gradingStructure === null || gradingStructure === void 0 ? void 0 : gradingStructure.questions) === null || _a === void 0 ? void 0 : _a.find(q => q.displayNumber === displayNumber);
    const questionId = question && ('choices' in question ? question.choices[0].id : question.id);
    const isNonAnswer = questionId ? (_b = answersByQuestionId[questionId]) === null || _b === void 0 ? void 0 : _b.answerNonAnswer : false;
    return (React.createElement("div", { className: classNames('e-exam-question', `e-level-${level}`, {
            'e-mrg-b-8 e-clearfix': level === 0,
            'e-mrg-l-8 e-mrg-y-4': level > 0,
            'no-answer': !hasAnswers,
            'non-answer': isNonAnswer
        }), id: displayNumber, "aria-description": !hasAnswers ? i18n.t('examineExam.questionHasNoAnswer') : undefined },
        renderChildNodes(element),
        React.createElement(AnnotationList, null)));
}
function questionHasAnswers(element, answers) {
    const answerElems = findChildrenAnswers(element);
    return answerElems.some(e => { var _a; return (_a = answers[getNumericAttribute(e, 'question-id')]) === null || _a === void 0 ? void 0 : _a.value; });
}
export default React.memo(withQuestionContext(Question));
//# sourceMappingURL=Question.js.map