"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useMarkdownTransform = useMarkdownTransform;
const tslib_1 = require("tslib");
const react_1 = require("react");
const transform_1 = tslib_1.__importDefault(require("@diplodoc/transform"));
require("@diplodoc/transform/dist/js/yfm.js");
const markdownUtils_1 = require("../utils/markdownUtils.js");
const parse_blocks_1 = require("../utils/parse-blocks.js");
function useMarkdownTransform(content, options) {
    const cacheRef = (0, react_1.useRef)(new Map());
    const prevOptionsRef = (0, react_1.useRef)(options);
    return (0, react_1.useMemo)(() => {
        if (!content) {
            return '';
        }
        const optionsChanged = !(0, markdownUtils_1.areOptionsEqual)(prevOptionsRef.current, options);
        if (optionsChanged) {
            cacheRef.current.clear();
            prevOptionsRef.current = options;
        }
        try {
            const blocks = (0, parse_blocks_1.parseMarkdownIntoBlocks)(content);
            const cache = cacheRef.current;
            const htmlParts = [];
            for (const block of blocks) {
                let html = cache.get(block);
                if (!html) {
                    try {
                        const result = (0, transform_1.default)(block, options);
                        html = result.result.html;
                        cache.set(block, html);
                    }
                    catch (_a) {
                        html = '';
                    }
                }
                htmlParts.push(html);
            }
            const currentBlocksSet = new Set(blocks);
            for (const key of cache.keys()) {
                if (!currentBlocksSet.has(key)) {
                    cache.delete(key);
                }
            }
            return htmlParts.join('');
        }
        catch (_b) {
            return '';
        }
    }, [content, options]);
}
//# sourceMappingURL=useMarkdownTransform.js.map
