import React, { useMemo, useRef } from "react";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { JINJA_DOLLAR_PLACEHOLDER, preprocessNormalizeLeafToContainerDirective } from "../../MarkdownEditor/editor/parser/constants";
import { buildEditorAlignedComponents, createHastProcessor, splitMarkdownBlocks } from "../markdownReactShared";
import { MarkdownBlockPiece } from "./MarkdownBlockPiece";
import { shouldResetRevisionProgress } from "./revisionPolicy";
import { useShallowMemo } from "./useShallowMemo";
/**
 * 流式优先的 Markdown → React：每块独立 MarkdownBlockPiece，末块 tail、其余 sealed。
 * 块 key 仅用修订代 + 下标，使「末块晋升为 sealed」时外层组件类型不变，避免子树卸载重挂。
 */ export var useStreamingMarkdownReact = function useStreamingMarkdownReact(content, options) {
    var revisionSource = (options === null || options === void 0 ? void 0 : options.contentRevisionSource) !== undefined ? options.contentRevisionSource : content;
    var prevRevisionRef = useRef(undefined);
    var revisionGenerationRef = useRef(0);
    var processor = useMemo(function() {
        return createHastProcessor(options === null || options === void 0 ? void 0 : options.remarkPlugins, options === null || options === void 0 ? void 0 : options.htmlConfig);
    }, [
        options === null || options === void 0 ? void 0 : options.remarkPlugins,
        options === null || options === void 0 ? void 0 : options.htmlConfig
    ]);
    var prefixCls = (options === null || options === void 0 ? void 0 : options.prefixCls) || 'ant-agentic-md-editor';
    var stableComponents = useShallowMemo(options === null || options === void 0 ? void 0 : options.components);
    var stableFncProps = useShallowMemo(options === null || options === void 0 ? void 0 : options.fncProps);
    var components = useMemo(function() {
        return buildEditorAlignedComponents(prefixCls, stableComponents || {}, options === null || options === void 0 ? void 0 : options.streaming, options === null || options === void 0 ? void 0 : options.linkConfig, stableFncProps, options === null || options === void 0 ? void 0 : options.streamingParagraphAnimation, options === null || options === void 0 ? void 0 : options.eleRender);
    }, [
        prefixCls,
        stableComponents,
        options === null || options === void 0 ? void 0 : options.streaming,
        options === null || options === void 0 ? void 0 : options.linkConfig,
        stableFncProps,
        options === null || options === void 0 ? void 0 : options.streamingParagraphAnimation,
        options === null || options === void 0 ? void 0 : options.eleRender
    ]);
    return useMemo(function() {
        if (!content) {
            prevRevisionRef.current = '';
            return null;
        }
        var prevRev = prevRevisionRef.current;
        if (prevRev !== undefined && shouldResetRevisionProgress(prevRev, revisionSource)) {
            revisionGenerationRef.current += 1;
        }
        prevRevisionRef.current = revisionSource;
        try {
            var preprocessed = preprocessNormalizeLeafToContainerDirective(content.replace(new RegExp(JINJA_DOLLAR_PLACEHOLDER, 'g'), '$'));
            var blocks = splitMarkdownBlocks(preprocessed);
            if (blocks.length === 0) return null;
            var gen = revisionGenerationRef.current;
            var elements = blocks.map(function(blockSource, index) {
                var isLast = index === blocks.length - 1;
                var key = "b-".concat(gen, "-").concat(index);
                return jsx(MarkdownBlockPiece, {
                    variant: isLast ? 'tail' : 'sealed',
                    blockSource: blockSource,
                    processor: processor,
                    components: components,
                    streaming: !!(options === null || options === void 0 ? void 0 : options.streaming)
                }, key);
            });
            return jsxs(Fragment, {
                children: elements
            });
        } catch (error) {
            console.error('Failed to render markdown:', error);
            return null;
        }
    }, [
        content,
        revisionSource,
        processor,
        components,
        options === null || options === void 0 ? void 0 : options.streaming
    ]);
};
