"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get renderCustomToken () {
        return renderCustomToken;
    },
    get renderInlineTokens () {
        return renderInlineTokens;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _renderers = require("./renderers");
const _renderUtils = require("./renderUtils");
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const renderCustomToken = (markedEngine, tokenRenderer, token, key, inline)=>{
    const extensionRenderer = markedEngine.defaults.extensions?.renderers?.[token.type];
    let children;
    if ('tokens' in token && Array.isArray(token.tokens)) {
        children = renderInlineTokens(markedEngine, tokenRenderer, token.tokens, `${key}-custom`);
    } else if ('text' in token && typeof token.text === 'string') {
        children = token.text;
    }
    const customNode = tokenRenderer?.(token, children);
    if (customNode !== undefined) {
        return customNode;
    }
    if (typeof extensionRenderer === 'function') {
        const parser = new markedEngine.Parser(markedEngine.defaults);
        const html = extensionRenderer.call({
            parser
        }, token);
        if (html !== false && html != null) {
            const tagName = !inline && 'block' in token && token.block ? 'div' : 'span';
            return (0, _renderUtils.renderSanitizedHtml)(key, String(html), tagName);
        }
    }
    return null;
};
const renderInlineTokens = (markedEngine, tokenRenderer, tokensList = [], keyPrefix)=>{
    const content = [];
    tokensList.forEach((token, index)=>{
        const key = `${keyPrefix}-${index}`;
        switch(token.type){
            case 'strong':
                {
                    const children = renderInlineTokens(markedEngine, tokenRenderer, token.tokens, `${key}-strong`);
                    const defaultNode = (0, _renderUtils.renderElement)(key, 'strong', undefined, children);
                    content.push((0, _renderUtils.resolveTokenNode)(tokenRenderer, token, defaultNode, children));
                }
                return;
            case 'em':
                {
                    const children = renderInlineTokens(markedEngine, tokenRenderer, token.tokens, `${key}-em`);
                    const defaultNode = (0, _renderUtils.renderElement)(key, 'em', undefined, children);
                    content.push((0, _renderUtils.resolveTokenNode)(tokenRenderer, token, defaultNode, children));
                }
                return;
            case 'del':
                {
                    const children = renderInlineTokens(markedEngine, tokenRenderer, token.tokens, `${key}-del`);
                    const defaultNode = (0, _renderUtils.renderElement)(key, 'del', undefined, children);
                    content.push((0, _renderUtils.resolveTokenNode)(tokenRenderer, token, defaultNode, children));
                }
                return;
            case 'codespan':
                {
                    const children = token.text;
                    const defaultNode = (0, _renderers.renderCode)(key, {
                        ...token,
                        children,
                        inline: true
                    });
                    content.push((0, _renderUtils.resolveTokenNode)(tokenRenderer, token, defaultNode, children));
                }
                return;
            case 'br':
                content.push((0, _renderUtils.resolveTokenNode)(tokenRenderer, token, (0, _renderUtils.renderElement)(key, 'br')));
                return;
            case 'link':
                {
                    const children = renderInlineTokens(markedEngine, tokenRenderer, token.tokens, `${key}-link`);
                    const defaultNode = (0, _renderUtils.renderElement)(key, _renderers.LinkRenderer, {
                        ...token,
                        href: token.href,
                        title: token.title || undefined
                    }, children);
                    content.push((0, _renderUtils.resolveTokenNode)(tokenRenderer, token, defaultNode, children));
                }
                return;
            case 'image':
                content.push((0, _renderUtils.resolveTokenNode)(tokenRenderer, token, (0, _renderUtils.renderElement)(key, _renderers.ImageRenderer, {
                    ...token,
                    alt: token.text,
                    src: token.href,
                    title: token.title || undefined
                })));
                return;
            case 'text':
                if (token.tokens?.length) {
                    content.push(/*#__PURE__*/ _react.default.createElement(_react.Fragment, {
                        key: key
                    }, renderInlineTokens(markedEngine, tokenRenderer, token.tokens, `${key}-text`)));
                    return;
                }
                content.push(token.text);
                return;
            case 'escape':
                content.push(token.text);
                return;
            case 'html':
                content.push((0, _renderUtils.renderSanitizedHtml)(key, token.raw, token.block ? 'div' : 'span'));
                return;
            default:
                {
                    const customToken = renderCustomToken(markedEngine, tokenRenderer, token, key, true);
                    if (customToken) {
                        content.push(customToken);
                        return;
                    }
                }
                if ('tokens' in token && Array.isArray(token.tokens)) {
                    content.push(/*#__PURE__*/ _react.default.createElement(_react.Fragment, {
                        key: key
                    }, renderInlineTokens(markedEngine, tokenRenderer, token.tokens, `${key}-fallback`)));
                    return;
                }
                if ('text' in token && typeof token.text === 'string') {
                    content.push(token.text);
                }
        }
    });
    return content;
};
