import React, { Fragment } from "react";
import { renderCode } from "./renderers";
import { renderCustomToken, renderInlineTokens } from "./renderInlineTokens";
import { renderTable } from "./renderTable";
import { renderElement, renderSanitizedHtml, resolveTokenNode } from "./renderUtils";
export const renderBlockTokens = (markedEngine, tokenRenderer, tokensList = [], context)=>{
    const content = [];
    tokensList.forEach((token, index)=>{
        const key = `${context.keyPrefix}-${index}`;
        switch(token.type){
            case 'space':
            case 'def':
                return;
            case 'paragraph':
                {
                    const children = renderInlineTokens(markedEngine, tokenRenderer, token.tokens, `${key}-paragraph`);
                    const defaultNode = renderElement(key, 'p', undefined, children);
                    content.push(resolveTokenNode(tokenRenderer, token, defaultNode, children));
                }
                return;
            case 'heading':
                {
                    const depth = Math.min(Math.max(token.depth, 1), 6);
                    const tagName = `h${depth}`;
                    const children = renderInlineTokens(markedEngine, tokenRenderer, token.tokens, `${key}-heading`);
                    const defaultNode = renderElement(key, tagName, undefined, children);
                    content.push(resolveTokenNode(tokenRenderer, token, defaultNode, children));
                    return;
                }
            case 'blockquote':
                {
                    const children = renderBlockTokens(markedEngine, tokenRenderer, token.tokens, {
                        keyPrefix: `${key}-blockquote`
                    });
                    const defaultNode = renderElement(key, 'blockquote', undefined, children);
                    content.push(resolveTokenNode(tokenRenderer, token, defaultNode, children));
                }
                return;
            case 'code':
                {
                    const children = token.text;
                    const defaultNode = renderCode(key, {
                        ...token,
                        children,
                        className: token.lang ? `language-${token.lang}` : undefined,
                        inline: false
                    });
                    content.push(resolveTokenNode(tokenRenderer, token, defaultNode, children));
                }
                return;
            case 'list':
                {
                    const listToken = token;
                    const listTag = listToken.ordered ? 'ol' : 'ul';
                    const start = listToken.ordered && listToken.start !== '' ? Number(listToken.start) : undefined;
                    const children = listToken.items.map((item, itemIndex)=>{
                        const itemChildren = renderBlockTokens(markedEngine, tokenRenderer, item.tokens, {
                            inTightListItem: !item.loose,
                            keyPrefix: `${key}-item-${itemIndex}`
                        });
                        let childContent = itemChildren;
                        if (item.task) {
                            childContent = [
                                /*#__PURE__*/ React.createElement("input", {
                                    checked: Boolean(item.checked),
                                    key: `${key}-item-${itemIndex}-checkbox`,
                                    type: "checkbox",
                                    disabled: true,
                                    readOnly: true
                                }),
                                ' ',
                                ...itemChildren
                            ];
                        }
                        const itemDefaultNode = renderElement(`${key}-li-${itemIndex}`, 'li', undefined, childContent);
                        return resolveTokenNode(tokenRenderer, item, itemDefaultNode, childContent);
                    });
                    const defaultNode = renderElement(key, listTag, start === undefined ? undefined : {
                        start
                    }, children);
                    content.push(resolveTokenNode(tokenRenderer, token, defaultNode, children));
                    return;
                }
            case 'hr':
                content.push(resolveTokenNode(tokenRenderer, token, renderElement(key, 'hr')));
                return;
            case 'table':
                content.push(renderTable(markedEngine, tokenRenderer, token, key));
                return;
            case 'html':
                content.push(renderSanitizedHtml(key, token.raw, token.block ? 'div' : 'span'));
                return;
            case 'text':
                if (token.tokens?.length) {
                    const textContent = renderInlineTokens(markedEngine, tokenRenderer, token.tokens, `${key}-text`);
                    if (context.inTightListItem) {
                        content.push(/*#__PURE__*/ React.createElement(Fragment, {
                            key: key
                        }, textContent));
                    } else {
                        content.push(renderElement(key, 'p', undefined, textContent));
                    }
                    return;
                }
                if (context.inTightListItem) {
                    content.push(/*#__PURE__*/ React.createElement(Fragment, {
                        key: key
                    }, token.text));
                } else {
                    content.push(renderElement(key, 'p', undefined, token.text));
                }
                return;
            default:
                {
                    const customToken = renderCustomToken(markedEngine, tokenRenderer, token, key);
                    if (customToken) {
                        content.push(customToken);
                        return;
                    }
                }
                if ('tokens' in token && Array.isArray(token.tokens)) {
                    content.push(...renderBlockTokens(markedEngine, tokenRenderer, token.tokens, {
                        ...context,
                        keyPrefix: `${key}-fallback`
                    }));
                }
        }
    });
    return content;
};
