import React from 'react';
import { getElementPath, mapChildNodes } from './dom-utils';
export const ExamNamespaceURI = 'http://ylioppilastutkinto.fi/exam.xsd';
export const XHTMLNamespaceURI = 'http://www.w3.org/1999/xhtml';
export function createRenderChildNodes(componentMap) {
    return (renderComponentOverrides = {}) => {
        function renderChildNode(node, index, options) {
            switch (node.nodeType) {
                case Node.TEXT_NODE:
                case Node.CDATA_SECTION_NODE: {
                    return options === 0 /* RenderOptions.RenderHTML */ ? renderTextNode(node) : null;
                }
                case Node.ELEMENT_NODE:
                    return renderElement(node, index, options);
                default:
                    return null;
            }
        }
        function renderTextNode(element) {
            const Component = renderComponentOverrides['text'];
            return Component ? (React.createElement(Component, { key: getElementPath(element), element: element, renderChildNodes: renderChildNodes, renderComponentOverrides: renderComponentOverrides })) : (element.textContent);
        }
        function renderElement(element, index, options) {
            switch (element.namespaceURI) {
                case XHTMLNamespaceURI:
                    return renderXHTMLElement(element, index, options);
                case ExamNamespaceURI:
                    return renderExamElement(element, index, options);
                default:
                    throw new Error(`Unrecognized element: ${element.localName}`);
            }
        }
        function renderXHTMLElement(element, index, options) {
            const children = element.hasChildNodes() ? renderChildNodes(element, options) : null;
            if (options === 0 /* RenderOptions.RenderHTML */) {
                const Tag = element.localName;
                const props = htmlAttributes2props(element, index);
                return React.createElement(Tag, { ...props }, children);
            }
            else {
                return children;
            }
        }
        function renderExamElement(element, index, options) {
            var _a;
            if (!(element.localName in componentMap)) {
                return null;
            }
            const elementComponentDefinition = componentMap[element.localName];
            const Wrapper = 'wrapper' in elementComponentDefinition ? elementComponentDefinition.wrapper : undefined;
            const Component = (_a = renderComponentOverrides[element.localName]) !== null && _a !== void 0 ? _a : ('component' in elementComponentDefinition ? elementComponentDefinition.component : elementComponentDefinition);
            const className = element.getAttribute('class') || undefined;
            const content = (React.createElement(Component, { element,
                className,
                renderChildNodes,
                renderComponentOverrides,
                key: Wrapper ? undefined : key(element, index),
                options }));
            return Wrapper ? (React.createElement(Wrapper, { key: key(element, index), element: element }, content)) : (content);
        }
        function renderChildNodes(element, options = 0 /* RenderOptions.RenderHTML */) {
            return mapChildNodes(element, (childElement, i) => renderChildNode(childElement, i, options));
        }
        return renderChildNodes;
    };
}
function htmlAttributes2props(element, index) {
    const props = { key: key(element, index) };
    for (const attribute of element.attributes) {
        const name = attribute.localName === 'class' ? 'className' : attribute.localName;
        props[name] = attribute.value;
    }
    return props;
}
function key(element, index) {
    return `${element.localName}-${index}`;
}
//# sourceMappingURL=createRenderChildNodes.js.map