"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importDefault(require("react"));
const sortInlineBlocks = (inlineBlocks) => {
    return inlineBlocks.sort((a, b) => a.start - b.start);
};
const renderInlineBlock = (text, type, renderers, index = 0) => {
    if (!type[0])
        return text;
    const CurrentRenderer = renderers === null || renderers === void 0 ? void 0 : renderers[type[0]];
    if (!CurrentRenderer)
        return text;
    return (<span data-type={type[0]} key={index}>
      <CurrentRenderer>
        {renderInlineBlock(text, type.slice(1), renderers)}
      </CurrentRenderer>
    </span>);
};
const renderInlineBlocks = (text, inlineBlocks, renderers) => {
    let result = [];
    let start = 0;
    sortInlineBlocks(inlineBlocks || []).forEach((block, index) => {
        const { type, start: blockStart, end: blockEnd } = block;
        const preBlockText = text.slice(start, blockStart);
        const blockText = text.slice(blockStart, blockEnd);
        if (preBlockText) {
            result.push(preBlockText);
        }
        result.push(renderInlineBlock(blockText, type, renderers, index));
        start = blockEnd;
    });
    if (start >= text.length) {
        if (text.length === 0) {
            return ['\n'];
        }
        return result;
    }
    const trailingText = text
        .substring(start);
    result.push(trailingText.endsWith('\n')
        ? trailingText
        : trailingText + '\n');
    return result;
};
exports.default = renderInlineBlocks;
//# sourceMappingURL=renderInlineBlocks.jsx.map