import React, { useContext, useEffect, useMemo } from 'react';
import { I18nextProvider } from 'react-i18next';
import { createRenderChildNodes } from '../../createRenderChildNodes';
import { NBSP, findChildElement } from '../../dom-utils';
import { changeLanguage, initI18n, useExamTranslation } from '../../i18n';
import { examTitleId } from '../../ids';
import { scrollToHash } from '../../scrollToHash';
import { useCached } from '../../useCached';
import DocumentTitle from '../DocumentTitle';
import RenderChildNodes from '../RenderChildNodes';
import RenderExamElements from '../RenderExamElements';
import SectionElement from '../SectionElement';
import { AnnotationProvider } from '../context/AnnotationProvider';
import { CommonExamContext, withCommonExamContext } from '../context/CommonExamContext';
import { withExamContext } from '../context/ExamContext';
import { withSectionContext } from '../context/SectionContext';
import AttachmentsExternalMaterial from './ExternalMaterial';
import AttachmentsQuestion from './Question';
import AttachmentsQuestionTitle from './QuestionTitle';
const _renderChildNodes = createRenderChildNodes({
    'audio-group': RenderExamElements,
    'external-material': AttachmentsExternalMaterial,
    'question-title': AttachmentsQuestionTitle,
    question: AttachmentsQuestion,
    section: withSectionContext(RenderChildNodes)
});
const Attachments = ({ annotationsEnabled, renderComponentOverrides = {} }) => {
    const { root, language, date, dateTimeFormatter, dayCode, examCode, resolveAttachment, subjectLanguage } = useContext(CommonExamContext);
    const renderChildNodes = useMemo(() => _renderChildNodes(renderComponentOverrides), [renderComponentOverrides]);
    const examTitle = findChildElement(root, 'exam-title');
    const examStylesheet = root.getAttribute('exam-stylesheet');
    const externalMaterial = findChildElement(root, 'external-material');
    const i18n = useCached(() => initI18n(language, examCode, dayCode));
    useEffect(changeLanguage(i18n, language));
    useEffect(scrollToHash, []);
    return (React.createElement(AnnotationProvider, { annotationsEnabled: annotationsEnabled },
        React.createElement(I18nextProvider, { i18n: i18n },
            React.createElement("main", { className: "e-exam attachments", lang: subjectLanguage, "aria-labelledby": examTitleId },
                React.createElement(React.StrictMode, null),
                examStylesheet && React.createElement("link", { rel: "stylesheet", href: resolveAttachment(examStylesheet) }),
                React.createElement(SectionElement, { "aria-labelledby": examTitleId },
                    examTitle && (React.createElement(DocumentTitle, { id: examTitleId },
                        React.createElement(AttachmentsPageTitle, null),
                        NBSP,
                        renderChildNodes(examTitle))),
                    date && React.createElement("div", { className: "e-semibold e-mrg-b-6" }, dateTimeFormatter.format(date)),
                    externalMaterial && (React.createElement(AttachmentsExternalMaterial, { element: externalMaterial, renderChildNodes, renderComponentOverrides, forceRender: true }))),
                renderChildNodes(root)))));
};
// Wrap to a separate component, since we can't use translation hooks in the root component.
export const AttachmentsPageTitle = () => {
    const { t } = useExamTranslation();
    return React.createElement(React.Fragment, null, t('attachments-page-title'));
};
export default React.memo(withExamContext(withCommonExamContext(Attachments)));
//# sourceMappingURL=Attachments.js.map