"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, properties, type, mutations, inlineBlockInfo, renderers, index = 0) => {
    if (!type[0])
        return text;
    const Rendererer = renderers === null || renderers === void 0 ? void 0 : renderers[type[0]];
    if (!Rendererer)
        return text;
    return (<span data-type={type[0]} data-properties={JSON.stringify(properties[0])} key={index}>
      <Rendererer properties={Object.assign(Object.assign({}, properties[0]), inlineBlockInfo)} mutations={mutations}>
        {renderInlineBlock(text, properties.slice(1), type.slice(1), mutations, inlineBlockInfo, renderers)}
      </Rendererer>
    </span>);
};
const renderInlineBlocks = (text, mutations, blockID, inlineBlocks, rendererers) => {
    let result = [];
    let start = 0;
    sortInlineBlocks(inlineBlocks || []).forEach((block, index) => {
        const { type, properties, 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, properties || [], type, mutations, {
            blockID,
            blockStart,
            blockEnd,
        }, rendererers, index));
        start = blockEnd;
    });
    if (start >= text.length) {
        if (text.length === 0) {
            return ['\uFEFF'];
        }
        result.push(<span>
        {'\uFEFF'}
      </span>);
        return result;
    }
    const trailingText = text
        .substring(start);
    result.push(trailingText.endsWith('\uFEFF')
        ? trailingText
        : trailingText + '\uFEFF');
    return result;
};
exports.default = renderInlineBlocks;
//# sourceMappingURL=renderInlineBlocks.jsx.map