function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
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 _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
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;
}
function _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
import { memo, useContext, useMemo } from "react";
import { ConfigProvider, Flex } from "antd";
import clsx from "clsx";
import React from "react";
import { Quote } from "../Quote";
import { BubbleConfigContext } from "./BubbleConfigProvide";
import { ContentFilemapView } from "./ContentFilemapView";
import { BubbleMessageDisplay } from "./MessagesContent";
import { MessagesContext } from "./MessagesContent/BubbleContext";
import { BubbleExtra } from "./MessagesContent/BubbleExtra";
import { extractFilemapBlocks } from "./extractFilemapBlocks";
import { useStyle } from "./style";
import { runRender } from "./AIBubble";
import { BubbleFileView } from "./FileView";
import { BubbleTitle } from "./Title";
var USER_PLACEMENT = 'right';
var BUBBLE_GAP = 12;
var FILE_VIEW_PADDING_LEFT = 12;
var getContentContainerStyle = function getContentContainerStyle() {
    return {
        display: 'flex',
        gap: 4,
        flexDirection: 'column',
        alignItems: 'flex-end'
    };
};
var getFileViewStyle = function getFileViewStyle(standalone, customStyle) {
    return _object_spread({
        minWidth: standalone ? 'min(296px,100%)' : '0px',
        paddingLeft: FILE_VIEW_PADDING_LEFT
    }, customStyle);
};
var getContentStyle = function getContentStyle(standalone, customStyle) {
    return _object_spread({
        minWidth: standalone ? 'min(16px,100%)' : '0px'
    }, customStyle);
};
/**
 * UserBubble 组件
 *
 * 显示用户发送的消息，采用右侧布局
 *
 * @example
 * ```tsx
 * <UserBubble
 *   avatar={{ avatar: "url", title: "用户" }}
 *   time={new Date()}
 * >
 *   用户消息内容
 * </UserBubble>
 * ```
 */ export var UserBubble = /*#__PURE__*/ memo(function(props) {
    var _props_readonly;
    var _originData_fileMap, _props_markdownRenderConfig, _props_bubbleRenderConfig, _props_styles, _props_bubbleRenderConfig1, _bubbleRenderConfig_render;
    var className = props.className, style = props.style, bubbleRenderConfig = props.bubbleRenderConfig, classNames = props.classNames, styles = props.styles, originData = props.originData, quote = props.quote;
    var _React_useState = _sliced_to_array(React.useState(false), 2), hidePadding = _React_useState[0], setHidePadding = _React_useState[1];
    var getPrefixCls = useContext(ConfigProvider.ConfigContext).getPrefixCls;
    var context = useContext(BubbleConfigContext);
    var _ref = context || {}, compact = _ref.compact, standalone = _ref.standalone, extraShowOnHover = _ref.extraShowOnHover;
    var prefixClass = getPrefixCls('agentic');
    var _useStyle = useStyle(prefixClass, classNames), wrapSSR = _useStyle.wrapSSR, hashId = _useStyle.hashId;
    var time = (originData === null || originData === void 0 ? void 0 : originData.createAt) || props.time;
    var placement = USER_PLACEMENT;
    var hasFileMap = ((originData === null || originData === void 0 ? void 0 : (_originData_fileMap = originData.fileMap) === null || _originData_fileMap === void 0 ? void 0 : _originData_fileMap.size) || 0) > 0;
    var rawContent = originData === null || originData === void 0 ? void 0 : originData.content;
    var _useMemo = useMemo(function() {
        return extractFilemapBlocks(typeof rawContent === 'string' ? rawContent : '');
    }, [
        rawContent
    ]), filemapBlocks = _useMemo.blocks, strippedContent = _useMemo.stripped;
    var quoteElement = (quote === null || quote === void 0 ? void 0 : quote.quoteDescription) ? /*#__PURE__*/ React.createElement(Quote, quote) : null;
    var titleDom = runRender(bubbleRenderConfig === null || bubbleRenderConfig === void 0 ? void 0 : bubbleRenderConfig.titleRender, props, /*#__PURE__*/ React.createElement(BubbleTitle, {
        quote: quoteElement,
        bubbleNameClassName: classNames === null || classNames === void 0 ? void 0 : classNames.bubbleNameClassName,
        className: classNames === null || classNames === void 0 ? void 0 : classNames.bubbleListItemTitleClassName,
        style: styles === null || styles === void 0 ? void 0 : styles.bubbleListItemTitleStyle,
        prefixClass: clsx("".concat(prefixClass, "-bubble-title")),
        title: '',
        placement: placement,
        time: time
    }));
    var messageContent = /*#__PURE__*/ React.createElement(BubbleMessageDisplay, {
        markdownRenderConfig: props.markdownRenderConfig,
        docListProps: props.docListProps,
        bubbleListRef: props.bubbleListRef,
        bubbleListItemExtraStyle: styles === null || styles === void 0 ? void 0 : styles.bubbleListItemExtraStyle,
        bubbleRef: props.bubbleRef,
        content: filemapBlocks.length > 0 ? strippedContent : originData === null || originData === void 0 ? void 0 : originData.content,
        key: originData === null || originData === void 0 ? void 0 : originData.id,
        "data-id": originData === null || originData === void 0 ? void 0 : originData.id,
        avatar: originData === null || originData === void 0 ? void 0 : originData.meta,
        readonly: (_props_readonly = props.readonly) !== null && _props_readonly !== void 0 ? _props_readonly : false,
        onReply: props.onReply,
        id: props.id,
        originData: originData,
        placement: placement,
        time: (originData === null || originData === void 0 ? void 0 : originData.updateAt) || (originData === null || originData === void 0 ? void 0 : originData.createAt),
        customConfig: bubbleRenderConfig === null || bubbleRenderConfig === void 0 ? void 0 : bubbleRenderConfig.customConfig,
        pure: props.pure,
        shouldShowCopy: props.shouldShowCopy,
        fileViewEvents: props.fileViewEvents,
        fileViewConfig: props.fileViewConfig,
        renderFileMoreAction: props.renderFileMoreAction,
        bubbleRenderConfig: bubbleRenderConfig
    });
    var childrenDom = runRender(bubbleRenderConfig === null || bubbleRenderConfig === void 0 ? void 0 : bubbleRenderConfig.contentRender, props, messageContent);
    var contentBeforeDom = runRender(bubbleRenderConfig === null || bubbleRenderConfig === void 0 ? void 0 : bubbleRenderConfig.contentBeforeRender, props, null);
    var contentAfterDom = runRender(bubbleRenderConfig === null || bubbleRenderConfig === void 0 ? void 0 : bubbleRenderConfig.contentAfterRender, props, null);
    var contentContainerStyle = getContentContainerStyle();
    var fileViewStyle = getFileViewStyle(standalone, styles === null || styles === void 0 ? void 0 : styles.bubbleListItemExtraStyle);
    var contentStyle = getContentStyle(standalone, styles === null || styles === void 0 ? void 0 : styles.bubbleListItemContentStyle);
    // 用户气泡默认 hover 展示复制等操作，父级可透传覆盖
    var effectiveExtraShowOnHover = extraShowOnHover !== null && extraShowOnHover !== void 0 ? extraShowOnHover : true;
    var _obj;
    var itemDom = wrapSSR(/*#__PURE__*/ React.createElement(BubbleConfigContext.Provider, {
        value: {
            compact: compact,
            standalone: !!standalone,
            extraShowOnHover: effectiveExtraShowOnHover,
            bubble: props
        }
    }, /*#__PURE__*/ React.createElement(Flex, {
        className: clsx(hashId, className, "".concat(prefixClass, "-bubble"), "".concat(prefixClass, "-bubble-").concat(placement), "".concat(prefixClass, "-bubble-user"), _define_property({}, "".concat(prefixClass, "-bubble-compact"), compact), classNames === null || classNames === void 0 ? void 0 : classNames.bubbleClassName),
        style: style,
        vertical: true,
        id: props.id,
        "data-id": props.id,
        gap: BUBBLE_GAP
    }, /*#__PURE__*/ React.createElement("div", {
        style: style,
        className: clsx("".concat(prefixClass, "-bubble-container"), hashId)
    }, /*#__PURE__*/ React.createElement("div", {
        style: contentContainerStyle,
        className: clsx("".concat(prefixClass, "-bubble-container"), "".concat(prefixClass, "-bubble-container-").concat(placement), "".concat(prefixClass, "-bubble-container-user"), _define_property({}, "".concat(prefixClass, "-bubble-container-pure"), props.pure), classNames === null || classNames === void 0 ? void 0 : classNames.bubbleContainerClassName, hashId),
        "data-testid": "chat-message"
    }, titleDom ? /*#__PURE__*/ React.createElement("div", {
        "data-testid": "bubble-avatar-title",
        className: clsx("".concat(prefixClass, "-bubble-avatar-title"), "".concat(prefixClass, "-bubble-avatar-title-").concat(placement), "".concat(prefixClass, "-bubble-avatar-title-ai"), classNames === null || classNames === void 0 ? void 0 : classNames.bubbleAvatarTitleClassName, hashId, (_obj = {}, _define_property(_obj, "".concat(prefixClass, "-bubble-avatar-title-pure"), props.pure), _define_property(_obj, "".concat(prefixClass, "-bubble-avatar-title-quote"), quote === null || quote === void 0 ? void 0 : quote.quoteDescription), _obj))
    }, titleDom) : null, contentBeforeDom && /*#__PURE__*/ React.createElement("div", {
        style: styles === null || styles === void 0 ? void 0 : styles.bubbleListItemExtraStyle,
        className: clsx("".concat(prefixClass, "-bubble-before"), "".concat(prefixClass, "-bubble-before-").concat(placement), "".concat(prefixClass, "-bubble-before-user"), hashId),
        "data-testid": "message-before"
    }, contentBeforeDom), childrenDom ? /*#__PURE__*/ React.createElement("div", {
        style: contentStyle,
        className: clsx("".concat(prefixClass, "-bubble-content"), "".concat(prefixClass, "-bubble-content-").concat(placement), "".concat(prefixClass, "-bubble-content-user"), _define_property({}, "".concat(prefixClass, "-bubble-content-pure"), props.pure), classNames === null || classNames === void 0 ? void 0 : classNames.bubbleListItemContentClassName, hashId),
        onDoubleClick: props.onDoubleClick,
        "data-testid": "message-content"
    }, childrenDom) : null, contentAfterDom)), hasFileMap && /*#__PURE__*/ React.createElement("div", {
        style: _object_spread_props(_object_spread({}, fileViewStyle), {
            alignSelf: 'flex-end'
        }),
        className: clsx("".concat(prefixClass, "-bubble-after"), "".concat(prefixClass, "-bubble-after-").concat(placement), "".concat(prefixClass, "-bubble-after-user"), hashId),
        "data-testid": "message-after"
    }, /*#__PURE__*/ React.createElement(BubbleFileView, {
        bubbleListRef: props.bubbleListRef,
        bubble: props,
        placement: placement
    })), filemapBlocks.length > 0 && /*#__PURE__*/ React.createElement(ContentFilemapView, {
        blocks: filemapBlocks,
        fileViewConfig: props.fileViewConfig,
        fileViewEvents: props.fileViewEvents,
        fileMapConfig: (_props_markdownRenderConfig = props.markdownRenderConfig) === null || _props_markdownRenderConfig === void 0 ? void 0 : _props_markdownRenderConfig.fileMapConfig,
        placement: placement,
        style: {
            alignSelf: 'flex-end'
        }
    }))));
    if ((bubbleRenderConfig === null || bubbleRenderConfig === void 0 ? void 0 : bubbleRenderConfig.render) === false) return null;
    return /*#__PURE__*/ React.createElement(MessagesContext.Provider, {
        value: {
            message: props.originData,
            hidePadding: hidePadding,
            setHidePadding: setHidePadding,
            setMessage: function setMessage(message) {
                var _props_bubbleRef_current_setMessageItem, _props_bubbleRef_current, _props_bubbleRef;
                props === null || props === void 0 ? void 0 : (_props_bubbleRef = props.bubbleRef) === null || _props_bubbleRef === void 0 ? void 0 : (_props_bubbleRef_current = _props_bubbleRef.current) === null || _props_bubbleRef_current === void 0 ? void 0 : (_props_bubbleRef_current_setMessageItem = _props_bubbleRef_current.setMessageItem) === null || _props_bubbleRef_current_setMessageItem === void 0 ? void 0 : _props_bubbleRef_current_setMessageItem.call(_props_bubbleRef_current, props.id, message);
            }
        }
    }, /*#__PURE__*/ React.createElement(React.Fragment, null, (bubbleRenderConfig === null || bubbleRenderConfig === void 0 ? void 0 : (_bubbleRenderConfig_render = bubbleRenderConfig.render) === null || _bubbleRenderConfig_render === void 0 ? void 0 : _bubbleRenderConfig_render.call(bubbleRenderConfig, props, {
        avatar: null,
        title: null,
        header: null,
        extra: (props === null || props === void 0 ? void 0 : (_props_bubbleRenderConfig = props.bubbleRenderConfig) === null || _props_bubbleRenderConfig === void 0 ? void 0 : _props_bubbleRenderConfig.extraRender) === false ? null : /*#__PURE__*/ React.createElement(BubbleExtra, {
            pure: true,
            style: (_props_styles = props.styles) === null || _props_styles === void 0 ? void 0 : _props_styles.bubbleListItemExtraStyle,
            readonly: props.readonly,
            rightRender: props === null || props === void 0 ? void 0 : (_props_bubbleRenderConfig1 = props.bubbleRenderConfig) === null || _props_bubbleRenderConfig1 === void 0 ? void 0 : _props_bubbleRenderConfig1.extraRightRender,
            shouldShowCopy: props.shouldShowCopy,
            useSpeech: props.useSpeech,
            shouldShowVoice: props.shouldShowVoice,
            bubble: props
        }),
        messageContent: messageContent,
        itemDom: itemDom
    }, itemDom)) || itemDom));
});
