import { JINJA_DOLLAR_PLACEHOLDER, preprocessNormalizeLeafToContainerDirective } from "../../MarkdownEditor/editor/parser/constants";
import React, { useMemo, useRef } from "react";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { buildEditorAlignedComponents, createHastProcessor, splitMarkdownBlocks } from "../markdownReactShared";
import { MarkdownBlockPiece } from "./MarkdownBlockPiece";
import { shouldResetRevisionProgress } from "./revisionPolicy";
/**
 * 流式优先的 Markdown → React：每块独立 memo 组件；tail 与 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 components = useMemo(function() {
        return buildEditorAlignedComponents(prefixCls, (options === null || options === void 0 ? void 0 : options.components) || {}, options === null || options === void 0 ? void 0 : options.streaming, options === null || options === void 0 ? void 0 : options.linkConfig, options === null || options === void 0 ? void 0 : options.streamingParagraphAnimation, options === null || options === void 0 ? void 0 : options.eleRender);
    }, [
        prefixCls,
        options === null || options === void 0 ? void 0 : options.components,
        options === null || options === void 0 ? void 0 : options.streaming,
        options === null || options === void 0 ? void 0 : options.linkConfig,
        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;
                // 仅用修订代 + 块下标作 key，避免末块随文本增长导致 identity 变化而整段 remount
                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) && isLast)
                }, 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
    ]);
};
