import React, { useContext } from 'react';
import { findChildElement, getBooleanAttribute, NBSP } from '../../dom-utils';
import { useExamTranslation } from '../../i18n';
import { CommonExamContext } from '../context/CommonExamContext';
import RenderChildNodes from '../RenderChildNodes';
import classNames from 'classnames';
function Reference({ element, renderChildNodes, renderComponentOverrides }) {
    const { t } = useExamTranslation();
    function renderWith(localName, Component) {
        const childElement = findChildElement(element, localName);
        if (childElement) {
            const key = childElement.localName;
            const content = React.createElement(Component, { element: childElement, renderChildNodes, renderComponentOverrides, key });
            if (content) {
                const isHidden = getBooleanAttribute(childElement, 'hidden');
                return isHidden ? React.createElement("del", { key: key }, content) : content;
            }
        }
    }
    function renderWithPrefix(localName, translationKey, Component) {
        const childElement = findChildElement(element, localName);
        return (childElement && (React.createElement(React.Fragment, { key: childElement.localName },
            React.createElement("span", null,
                t(translationKey),
                " "),
            React.createElement(Component, { element: childElement, renderChildNodes, renderComponentOverrides, key: childElement.localName }))));
    }
    return (React.createElement("span", { className: classNames('e-break-word', { 'e-line-through': getBooleanAttribute(element, 'hidden') }) },
        React.createElement("span", null,
            t('references.source'),
            NBSP),
        intersperse('. ', [
            renderWith('author', RenderChildNodes),
            renderWith('title', Italic),
            renderWith('publisher', RenderChildNodes),
            renderWith('publication', RenderChildNodes),
            renderWith('howpublished', RenderChildNodes),
            renderWith('url', Link),
            renderWithPrefix('publication-date', 'references.date', AsDate),
            renderWithPrefix('reference-date', 'references.reference-date', AsDate),
            renderWithPrefix('translator', 'references.translator', RenderChildNodes),
            renderWithPrefix('modified-by', 'references.modified-by', RenderChildNodes),
            renderWith('note', RenderChildNodes)
        ])));
}
function Italic({ element, renderChildNodes }) {
    return React.createElement("em", null, renderChildNodes(element));
}
function Link({ element, renderChildNodes }) {
    return React.createElement("i", null, renderChildNodes(element));
}
function AsDate({ element }) {
    const textContent = element.textContent;
    if (/^[0-9]{4}$/.test(textContent)) {
        return React.createElement("span", null, textContent);
    }
    else {
        // The XML tag might contain leading or trailing whitespace, which breaks Date parsing.
        const date = new Date(textContent.trim());
        const { dateTimeFormatter } = useContext(CommonExamContext);
        return React.createElement("span", null, dateTimeFormatter.format(date));
    }
}
function intersperse(separator, contents) {
    return contents.filter(Boolean).reduce((acc, curr, i) => {
        acc.push(curr);
        if (i !== contents.length - 1) {
            acc.push(React.createElement("span", { key: i }, separator));
        }
        return acc;
    }, []);
}
export default React.memo(Reference);
//# sourceMappingURL=Reference.js.map