import { useMemo, useRef } from 'react';
import transform from '@diplodoc/transform';
import '@diplodoc/transform/dist/js/yfm.js';
import { areOptionsEqual } from "../utils/markdownUtils.js";
import { parseMarkdownIntoBlocks } from "../utils/parse-blocks.js";
export function useMarkdownTransform(content, options) {
    const cacheRef = useRef(new Map());
    const prevOptionsRef = useRef(options);
    return useMemo(() => {
        if (!content) {
            return '';
        }
        const optionsChanged = !areOptionsEqual(prevOptionsRef.current, options);
        if (optionsChanged) {
            cacheRef.current.clear();
            prevOptionsRef.current = options;
        }
        try {
            const blocks = parseMarkdownIntoBlocks(content);
            const cache = cacheRef.current;
            const htmlParts = [];
            for (const block of blocks) {
                let html = cache.get(block);
                if (!html) {
                    try {
                        const result = transform(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
