function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter(function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable;
            });
        }
        keys.push.apply(keys, symbols);
    }
    return keys;
}
function _object_spread_props(target, source) {
    source = source != null ? source : {};
    if (Object.getOwnPropertyDescriptors) {
        Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
    } else {
        ownKeys(Object(source)).forEach(function(key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
        });
    }
    return target;
}
import SkeletonList from "./SkeletonList";
import { useContext, useMemo, useRef } from "react";
import { ConfigProvider } from "antd";
import clsx from "clsx";
import { nanoid } from "nanoid";
import React from "react";
import { LazyElement } from "../../MarkdownEditor/editor/components/LazyElement";
import { BubbleConfigContext } from "../BubbleConfigProvide";
import { LOADING_FLAT } from "../MessagesContent";
import { PureAIBubble, PureUserBubble } from "../PureBubble";
import { useStyle } from "./style";
export var PureBubbleList = /*#__PURE__*/ React.memo(function(props) {
    var bubbleList = props.bubbleList, bubbleListRef = props.bubbleListRef, bubbleRenderConfig = props.bubbleRenderConfig, className = props.className, classNames = props.classNames, docListProps = props.docListProps, isLoading = props.isLoading, markdownRenderConfig = props.markdownRenderConfig, onAvatarClick = props.onAvatarClick, onCancelLike = props.onCancelLike, onLikeCancel = props.onLikeCancel, onDisLike = props.onDisLike, onDislike = props.onDislike, onDoubleClick = props.onDoubleClick, onLike = props.onLike, onReply = props.onReply, onScroll = props.onScroll, onTouchMove = props.onTouchMove, onWheel = props.onWheel, shouldShowCopy = props.shouldShowCopy, shouldShowVoice = props.shouldShowVoice, style = props.style, styles = props.styles, userMeta = props.userMeta, assistantMeta = props.assistantMeta;
    var getPrefixCls = useContext(ConfigProvider.ConfigContext).getPrefixCls;
    var compact = (useContext(BubbleConfigContext) || {}).compact;
    var prefixClass = getPrefixCls('agentic-bubble-list');
    var _useStyle = useStyle(prefixClass), wrapSSR = _useStyle.wrapSSR, hashId = _useStyle.hashId;
    var deps = useMemo(function() {
        return [
            props.style
        ];
    }, [
        props.style
    ]);
    var loadingKeysRef = useRef(new Map());
    var loadingKeyByIndexRef = useRef(new Map());
    var realIdToStableKeyRef = useRef(new Map());
    var listDom = useMemo(function() {
        var _props_lazy;
        var isLazyEnabled = (_props_lazy = props.lazy) === null || _props_lazy === void 0 ? void 0 : _props_lazy.enable;
        var totalCount = bubbleList.length;
        return bubbleList.map(function(item, index) {
            var placement = item.role === 'user' ? 'right' : 'left';
            var BubbleComponent = placement === 'right' ? PureUserBubble : PureAIBubble;
            var isLast = index === bubbleList.length - 1;
            var originDataWithFlags = _object_spread_props(_object_spread({}, item), {
                isLatest: isLast,
                isLast: isLast
            });
            var itemKey;
            if (item.id === LOADING_FLAT) {
                var cacheKey = "".concat(index, "-").concat(item.createAt || Date.now());
                if (!loadingKeysRef.current.has(cacheKey)) {
                    loadingKeysRef.current.set(cacheKey, nanoid());
                }
                itemKey = loadingKeysRef.current.get(cacheKey);
                loadingKeyByIndexRef.current.set(index, itemKey);
            } else {
                var realId = item.id;
                var prevLoadingKey = loadingKeyByIndexRef.current.get(index);
                if (prevLoadingKey) {
                    itemKey = prevLoadingKey;
                    realIdToStableKeyRef.current.set(realId, prevLoadingKey);
                    loadingKeyByIndexRef.current.delete(index);
                } else if (realIdToStableKeyRef.current.has(realId)) {
                    itemKey = realIdToStableKeyRef.current.get(realId);
                } else {
                    itemKey = realId;
                }
            }
            var bubbleElement = /*#__PURE__*/ React.createElement(BubbleComponent, {
                key: itemKey,
                "data-id": item.id,
                avatar: _object_spread({}, placement === 'right' ? userMeta : assistantMeta, item.meta),
                preMessage: bubbleList[index - 1],
                id: item.id,
                style: _object_spread({}, styles === null || styles === void 0 ? void 0 : styles.bubbleListItemStyle),
                originData: originDataWithFlags,
                placement: placement,
                time: item.updateAt || item.createAt,
                deps: deps,
                pure: true,
                bubbleListRef: bubbleListRef,
                bubbleRenderConfig: bubbleRenderConfig,
                classNames: classNames,
                bubbleRef: props.bubbleRef,
                markdownRenderConfig: markdownRenderConfig,
                docListProps: docListProps,
                styles: _object_spread_props(_object_spread({}, styles), {
                    bubbleListItemContentStyle: _object_spread({}, styles === null || styles === void 0 ? void 0 : styles.bubbleListItemContentStyle, placement === 'right' ? styles === null || styles === void 0 ? void 0 : styles.bubbleListRightItemContentStyle : styles === null || styles === void 0 ? void 0 : styles.bubbleListLeftItemContentStyle)
                }),
                readonly: props.readonly,
                onReply: onReply,
                onDislike: onDislike || onDisLike,
                onLike: onLike,
                onLikeCancel: onLikeCancel || onCancelLike,
                onAvatarClick: onAvatarClick,
                onDoubleClick: onDoubleClick,
                customConfig: bubbleRenderConfig === null || bubbleRenderConfig === void 0 ? void 0 : bubbleRenderConfig.customConfig,
                shouldShowCopy: shouldShowCopy,
                shouldShowVoice: shouldShowVoice
            });
            // 如果启用了懒加载，用 LazyElement 包裹
            if (isLazyEnabled) {
                var _ref, _ref1, _ref2;
                var _props_lazy_shouldLazyLoad, _props_lazy, _props_lazy1, _props_lazy2, _props_lazy3;
                // 检查是否应该对该消息启用懒加载
                var shouldLazyLoad = (_ref = (_props_lazy = props.lazy) === null || _props_lazy === void 0 ? void 0 : (_props_lazy_shouldLazyLoad = _props_lazy.shouldLazyLoad) === null || _props_lazy_shouldLazyLoad === void 0 ? void 0 : _props_lazy_shouldLazyLoad.call(_props_lazy, index, totalCount)) !== null && _ref !== void 0 ? _ref : true;
                // 如果不需要懒加载，直接返回元素
                if (!shouldLazyLoad) {
                    return bubbleElement;
                }
                // 创建适配的 renderPlaceholder，将 role 信息添加到 elementInfo
                var adaptedRenderPlaceholder = ((_props_lazy1 = props.lazy) === null || _props_lazy1 === void 0 ? void 0 : _props_lazy1.renderPlaceholder) ? function(lazyProps) {
                    return props.lazy.renderPlaceholder(_object_spread_props(_object_spread({}, lazyProps), {
                        elementInfo: lazyProps.elementInfo ? _object_spread_props(_object_spread({}, lazyProps.elementInfo), {
                            role: item.role
                        }) : undefined
                    }));
                } : undefined;
                return /*#__PURE__*/ React.createElement(LazyElement, {
                    key: itemKey,
                    placeholderHeight: (_ref1 = (_props_lazy2 = props.lazy) === null || _props_lazy2 === void 0 ? void 0 : _props_lazy2.placeholderHeight) !== null && _ref1 !== void 0 ? _ref1 : 100,
                    rootMargin: (_ref2 = (_props_lazy3 = props.lazy) === null || _props_lazy3 === void 0 ? void 0 : _props_lazy3.rootMargin) !== null && _ref2 !== void 0 ? _ref2 : '200px',
                    renderPlaceholder: adaptedRenderPlaceholder,
                    elementInfo: {
                        type: 'bubble',
                        index: index,
                        total: totalCount
                    }
                }, bubbleElement);
            }
            return bubbleElement;
        });
    }, [
        bubbleList,
        props.style,
        props.lazy,
        bubbleListRef,
        bubbleRenderConfig,
        classNames,
        props.bubbleRef,
        markdownRenderConfig,
        docListProps,
        styles,
        props.readonly,
        onReply,
        onDislike,
        onDisLike,
        onLike,
        onLikeCancel,
        onCancelLike,
        onAvatarClick,
        onDoubleClick,
        shouldShowCopy,
        shouldShowVoice,
        userMeta,
        assistantMeta,
        deps
    ]);
    if (isLoading) {
        return wrapSSR(/*#__PURE__*/ React.createElement("div", {
            className: clsx(prefixClass, "".concat(prefixClass, "-loading"), className, hashId),
            "data-testid": prefixClass,
            ref: bubbleListRef,
            style: {
                padding: 24
            }
        }, /*#__PURE__*/ React.createElement(SkeletonList, null)));
    }
    var _obj;
    return wrapSSR(/*#__PURE__*/ React.createElement("div", {
        className: clsx("".concat(prefixClass), className, hashId, (_obj = {}, _define_property(_obj, "".concat(prefixClass, "-readonly"), props.readonly), _define_property(_obj, "".concat(prefixClass, "-compact"), compact), _obj)),
        "data-chat-list": bubbleList.length,
        "data-testid": prefixClass,
        style: style,
        ref: bubbleListRef,
        onScroll: onScroll,
        onWheel: function onWheel1(event) {
            return onWheel === null || onWheel === void 0 ? void 0 : onWheel(event, (bubbleListRef === null || bubbleListRef === void 0 ? void 0 : bubbleListRef.current) || null);
        },
        onTouchMove: function onTouchMove1(event) {
            return onTouchMove === null || onTouchMove === void 0 ? void 0 : onTouchMove(event, (bubbleListRef === null || bubbleListRef === void 0 ? void 0 : bubbleListRef.current) || null);
        }
    }, listDom));
});
PureBubbleList.displayName = 'PureBubbleList';
export default PureBubbleList;
