import React, { memo, useMemo, useRef } from "react";
import { renderMarkdownBlock } from "../markdownReactShared";
import { StreamingAnimationContext } from "../StreamingAnimationContext";
import { shouldReparseLastBlock } from "./lastBlockThrottle";
/**
 * 块级渲染单元：sealed 块缓存不动，tail 块节流重解析。
 */ export var MarkdownBlockPiece = /*#__PURE__*/ memo(function MarkdownBlockPiece(param) {
    var variant = param.variant, blockSource = param.blockSource, processor = param.processor, components = param.components, streaming = param.streaming;
    var lastParsedRef = useRef(null);
    var cacheRef = useRef(new Map());
    var processorRef = useRef(null);
    /**
   * 宿主常把 `components: { __codeBlock: X }` 内联在每次 render，引用恒变。
   * 若列入 useMemo 依赖，末块晋升为 sealed 时会误触发重 parse，子树卸载重挂。
   * 密封命中缓存时故意不随 components 引用抖动；需重算时由 variant / blockSource / processor 驱动。
   */ var componentsRef = useRef(components);
    componentsRef.current = components;
    var node = useMemo(function() {
        if (processorRef.current !== processor) {
            processorRef.current = processor;
            cacheRef.current.clear();
            lastParsedRef.current = null;
        }
        var comps = componentsRef.current;
        if (variant === 'sealed') {
            var _lastParsedRef_current;
            var cached = cacheRef.current.get(blockSource);
            if (cached) return cached;
            // 末块刚晋升为 sealed 时 cacheRef 通常未命中（tail 路径只写 lastParsedRef）。
            // 直接复用 lastParsedRef 上一次 parse 的结果，避免再走一次 renderMarkdownBlock
            // 触发不必要的子树替换（chart / agentar-card 等重组件依赖 React 同位置同
            // 类型 reconciliation 来保留实例）。
            if (((_lastParsedRef_current = lastParsedRef.current) === null || _lastParsedRef_current === void 0 ? void 0 : _lastParsedRef_current.source) === blockSource) {
                var el = lastParsedRef.current.node;
                cacheRef.current.set(blockSource, el);
                return el;
            }
            var el1 = renderMarkdownBlock(blockSource, processor, comps);
            cacheRef.current.set(blockSource, el1);
            return el1;
        }
        // tail 块：不写入 cacheRef，仅用 lastParsedRef
        if (!streaming) {
            var el2 = renderMarkdownBlock(blockSource, processor, comps);
            lastParsedRef.current = {
                source: blockSource,
                node: el2
            };
            return el2;
        }
        var prev = lastParsedRef.current;
        if (prev && !shouldReparseLastBlock(prev.source, blockSource, true)) {
            return prev.node;
        }
        var el3 = renderMarkdownBlock(blockSource, processor, comps);
        lastParsedRef.current = {
            source: blockSource,
            node: el3
        };
        return el3;
    }, [
        variant,
        blockSource,
        processor,
        streaming
    ]);
    var animateBlock = variant === 'tail' && streaming;
    return /*#__PURE__*/ React.createElement(StreamingAnimationContext.Provider, {
        value: {
            animateBlock: animateBlock
        }
    }, node);
});
MarkdownBlockPiece.displayName = 'MarkdownBlockPiece';
