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_without_properties(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys, key, i;
    if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
        sourceKeys = Reflect.ownKeys(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
        return target;
    }
    target = _object_without_properties_loose(source, excluded);
    if (Object.getOwnPropertySymbols) {
        sourceKeys = Object.getOwnPropertySymbols(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
    }
    return target;
}
function _object_without_properties_loose(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
    for(i = 0; i < sourceKeys.length; i++){
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) continue;
        if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
        target[key] = 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 { ExportOutlined, RightOutlined } from "@ant-design/icons";
import { ConfigProvider, Descriptions, Drawer, Popover } from "antd";
import classNames from "classnames";
import dayjs from "dayjs";
import { motion } from "framer-motion";
import React, { useContext } from "react";
import { ActionIconBox } from "../../Components/ActionIconBox";
import { I18nContext } from "../../I18n";
import { BaseMarkdownEditor } from "../../MarkdownEditor/BaseMarkdownEditor";
import { BubbleConfigContext } from "../BubbleConfigProvide";
import { useStyle } from "./docInfoStyle";
var replaceAllPlaceHolder = function(str, placeholder) {
    var message = str;
    placeholder === null || placeholder === void 0 ? void 0 : placeholder.forEach(function(item) {
        message = message.replaceAll("`${".concat(item.placeholder, "}`"), "(".concat(item.url || item.doc_id, ")")).replaceAll("$".concat(item.placeholder), "(".concat(item.url || item.doc_id, ")")).replaceAll("$[".concat(item.placeholder, "]"), "(".concat(item.url || item.doc_id, ")"));
    });
    return str;
};
/**
 * 文档信息列表组件，用于展示引用的内容列表。
 *
 * 该组件提供以下功能：
 * - 展示引用文档的列表
 * - 支持展开/收起文档列表
 * - 提供文档预览功能
 * - 支持点击查看原文
 *
 * @component
 * @param {Object} props - 组件属性
 * @param {Array} props.reference_url_info_list - 引用URL信息列表，用于替换占位符
 * @param {Array<Object>} [props.options] - 文档信息选项列表，包含文档内容和元数据
 * @param {Function} [props.onOriginUrlClick] - 点击原文链接的回调函数
 * @param {Function} [props.render] - 自定义渲染函数，用于自定义列表项的渲染
 *
 * @returns {JSX.Element} 文档信息列表组件
 */ export var DocInfoList = function(_0) {
    var reference_url_info_list = _0.reference_url_info_list, props = _object_without_properties(_0, [
        "reference_url_info_list"
    ]);
    var _props_options;
    var _React_useState = _sliced_to_array(React.useState(true), 2), expanded = _React_useState[0], setExpanded = _React_useState[1];
    var configContext = useContext(ConfigProvider.ConfigContext);
    var baseCls = configContext === null || configContext === void 0 ? void 0 : configContext.getPrefixCls("agent-doc-info");
    var chatContext = useContext(BubbleConfigContext);
    var locale = useContext(I18nContext).locale;
    var _useStyle = useStyle(baseCls), wrapSSR = _useStyle.wrapSSR, hashId = _useStyle.hashId;
    var docInfoList = ((_props_options = props.options) === null || _props_options === void 0 ? void 0 : _props_options.filter(function(item) {
        return item;
    })) || [];
    var _React_useState1 = _sliced_to_array(React.useState(null), 2), docMeta = _React_useState1[0], setDocMeta = _React_useState1[1];
    return wrapSSR(/*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement(Drawer, {
        title: (locale === null || locale === void 0 ? void 0 : locale['chat.message.preview']) || '预览' + (docMeta === null || docMeta === void 0 ? void 0 : docMeta.doc_name),
        open: !!docMeta,
        onClose: function() {
            setDocMeta(null);
        },
        width: '40vw'
    }, /*#__PURE__*/ React.createElement(Descriptions, {
        column: 1,
        items: [
            {
                label: (locale === null || locale === void 0 ? void 0 : locale['docInfo.name']) || '名称',
                span: 1,
                children: (docMeta === null || docMeta === void 0 ? void 0 : docMeta.doc_name) || (docMeta === null || docMeta === void 0 ? void 0 : docMeta.answer)
            },
            {
                label: (locale === null || locale === void 0 ? void 0 : locale['docInfo.updateTime']) || '更新时间',
                span: 1,
                children: dayjs(docMeta === null || docMeta === void 0 ? void 0 : docMeta.upload_time).format('YYYY-MM-DD HH:mm:ss')
            },
            {
                label: (locale === null || locale === void 0 ? void 0 : locale['docInfo.type']) || '类型',
                span: 1,
                children: docMeta === null || docMeta === void 0 ? void 0 : docMeta.type
            },
            {
                label: (locale === null || locale === void 0 ? void 0 : locale['docInfo.content']) || '内容',
                span: 1,
                children: docMeta === null || docMeta === void 0 ? void 0 : docMeta.origin_text
            }
        ]
    })), /*#__PURE__*/ React.createElement("div", {
        style: {
            display: 'flex',
            gap: 4,
            flexWrap: 'wrap',
            flexDirection: 'column',
            maxWidth: '100%'
        },
        className: classNames(baseCls, hashId, _define_property({}, "".concat(baseCls, "-compact"), chatContext === null || chatContext === void 0 ? void 0 : chatContext.compact))
    }, /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(baseCls, "-label"), hashId, _define_property({}, "".concat(baseCls, "-label-compact"), chatContext === null || chatContext === void 0 ? void 0 : chatContext.compact)),
        onClick: function() {
            setExpanded(!expanded);
        }
    }, /*#__PURE__*/ React.createElement("span", {
        style: {
            fontSize: '0.9em'
        }
    }, (locale === null || locale === void 0 ? void 0 : locale['docInfo.referenceContent']) || '引用内容', ':', ' ', /*#__PURE__*/ React.createElement("span", {
        style: {
            fontWeight: 'bold'
        }
    }, docInfoList.length, ' '), (locale === null || locale === void 0 ? void 0 : locale['docInfo.items']) || '项'), /*#__PURE__*/ React.createElement("div", {
        style: {
            height: '1.6em',
            borderRadius: '12px',
            opacity: 1,
            background: '#FFFFFF',
            fontSize: '1em',
            boxSizing: 'border-box',
            border: '1px solid #E6ECF4',
            padding: '0px 16px'
        }
    }, /*#__PURE__*/ React.createElement("span", {
        style: {
            transition: 'all 0.3s cubic-bezier(0.4, 0, 0.2, 1)',
            fontSize: '0.9em',
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
            gap: 4
        }
    }, !expanded ? (locale === null || locale === void 0 ? void 0 : locale['docInfo.collapse']) || '收起' : (locale === null || locale === void 0 ? void 0 : locale['docInfo.expand']) || '展开', /*#__PURE__*/ React.createElement(RightOutlined, {
        style: {
            fontSize: '0.9em',
            transition: 'all 0.3s cubic-bezier(0.4, 0, 0.2, 1)',
            transform: expanded ? 'rotate(90deg)' : 'rotate(-90deg)'
        }
    })))), /*#__PURE__*/ React.createElement(motion.div, {
        layout: true,
        className: classNames("".concat(baseCls, "-list"), hashId),
        style: {
            height: expanded ? '0px' : 'auto',
            opacity: expanded ? 0 : 1,
            transition: 'all 0.3s cubic-bezier(0.4, 0, 0.2, 1)',
            border: expanded ? 'none' : '1px solid rgba(0,0,0,0.1)',
            borderRadius: '12px',
            display: 'flex',
            flexDirection: 'column',
            maxWidth: '100%',
            padding: expanded ? 0 : '12px',
            background: '#FBFCFD'
        },
        whileInView: "visible",
        variants: {
            hidden: {
                opacity: 0,
                scale: 0,
                transition: {
                    when: 'afterChildren'
                }
            },
            visible: {
                opacity: 1,
                scale: 1,
                transition: {
                    when: 'beforeChildren'
                }
            }
        },
        initial: "hidden",
        animate: "visible"
    }, docInfoList.map(function(item, index) {
        var _item_docMeta, _item_docMeta1, _item_content_trim, _item_content, _item_content1, _item_docMeta2, _props_render;
        var dom = /*#__PURE__*/ React.createElement(motion.div, {
            key: index,
            className: classNames("".concat(baseCls, "-list-item"), hashId),
            title: item === null || item === void 0 ? void 0 : item.content,
            onClick: function() {
                if (item === null || item === void 0 ? void 0 : item.originUrl) {
                    var _props_onOriginUrlClick;
                    return (_props_onOriginUrlClick = props.onOriginUrlClick) === null || _props_onOriginUrlClick === void 0 ? void 0 : _props_onOriginUrlClick.call(props, item.originUrl);
                }
                window.open(item.originUrl);
            },
            variants: {
                hidden: {
                    opacity: 0
                },
                visible: {
                    opacity: 1
                }
            }
        }, /*#__PURE__*/ React.createElement("div", {
            className: classNames("".concat(baseCls, "-list-item-title"), hashId),
            style: {
                width: '100%'
            }
        }, /*#__PURE__*/ React.createElement("div", {
            style: {
                lineHeight: '24px',
                borderRadius: 16,
                alignItems: 'center',
                maxWidth: '100%',
                display: 'flex',
                padding: 4
            }
        }, /*#__PURE__*/ React.createElement("img", {
            className: classNames("".concat(baseCls, "-list-item-icon"), hashId),
            src: 'https://mdn.alipayobjects.com/huamei_ptjqan/afts/img/A*kF_GTppRbp4AAAAAAAAAAAAADkN6AQ/original'
        })), /*#__PURE__*/ React.createElement("div", {
            style: {
                display: 'flex',
                flex: 1,
                flexDirection: 'column',
                maxWidth: 'calc(100% - 24px)'
            }
        }, /*#__PURE__*/ React.createElement("div", {
            style: {
                overflow: 'hidden',
                textOverflow: 'ellipsis',
                display: '-webkit-box',
                WebkitLineClamp: 1,
                lineHeight: '24px',
                WebkitBoxOrient: 'vertical',
                maxWidth: 'calc(100% - 24px)'
            }
        }, replaceAllPlaceHolder((item === null || item === void 0 ? void 0 : item.content) || '', reference_url_info_list || [])), (item === null || item === void 0 ? void 0 : (_item_docMeta = item.docMeta) === null || _item_docMeta === void 0 ? void 0 : _item_docMeta.doc_name) ? /*#__PURE__*/ React.createElement("div", {
            style: {
                fontSize: '12px',
                fontWeight: 'normal',
                lineHeight: '22px',
                textAlign: 'justify',
                color: '#B9C0CB',
                overflow: 'hidden',
                textOverflow: 'ellipsis',
                maxWidth: '100%',
                display: '-webkit-box',
                WebkitLineClamp: 1,
                WebkitBoxOrient: 'vertical'
            }
        }, item === null || item === void 0 ? void 0 : (_item_docMeta1 = item.docMeta) === null || _item_docMeta1 === void 0 ? void 0 : _item_docMeta1.doc_name) : null), (item === null || item === void 0 ? void 0 : item.originUrl) ? /*#__PURE__*/ React.createElement(ActionIconBox, {
            title: (locale === null || locale === void 0 ? void 0 : locale['chat.message.viewOriginal']) || '查看原文',
            style: {
                fontSize: '1em'
            },
            onClick: function(e) {
                e.stopPropagation();
                e.preventDefault();
                if (item === null || item === void 0 ? void 0 : item.originUrl) {
                    var _props_onOriginUrlClick;
                    return (_props_onOriginUrlClick = props.onOriginUrlClick) === null || _props_onOriginUrlClick === void 0 ? void 0 : _props_onOriginUrlClick.call(props, item.originUrl);
                }
                window.open(item.originUrl);
            }
        }, /*#__PURE__*/ React.createElement(ExportOutlined, null)) : null));
        if (((item === null || item === void 0 ? void 0 : (_item_content = item.content) === null || _item_content === void 0 ? void 0 : (_item_content_trim = _item_content.trim()) === null || _item_content_trim === void 0 ? void 0 : _item_content_trim.length) || 0) < 20) {
            return dom;
        }
        return /*#__PURE__*/ React.createElement(Popover, {
            key: index,
            placement: "left",
            content: /*#__PURE__*/ React.createElement("div", {
                style: {
                    width: 400,
                    maxHeight: 400,
                    overflow: 'auto',
                    display: 'flex',
                    flexDirection: 'column',
                    gap: 12
                }
            }, /*#__PURE__*/ React.createElement(BaseMarkdownEditor, {
                style: {
                    padding: 0,
                    width: '100%'
                },
                tableConfig: {
                    actions: {
                        fullScreen: 'modal'
                    }
                },
                readonly: true,
                contentStyle: {
                    padding: 0,
                    width: '100%'
                },
                initValue: item === null || item === void 0 ? void 0 : (_item_content1 = item.content) === null || _item_content1 === void 0 ? void 0 : _item_content1.trim()
            }), (item === null || item === void 0 ? void 0 : item.docMeta) ? /*#__PURE__*/ React.createElement("div", {
                style: {
                    borderRadius: '12px',
                    opacity: 1,
                    display: 'flex',
                    flexDirection: 'row',
                    alignItems: 'center',
                    padding: '10px',
                    gap: '10px',
                    alignSelf: 'stretch',
                    background: '#FBFCFD',
                    cursor: 'pointer',
                    zIndex: 1
                },
                onClick: function() {
                    setDocMeta(item.docMeta);
                }
            }, /*#__PURE__*/ React.createElement("img", {
                style: {
                    width: 24
                },
                src: 'https://mdn.alipayobjects.com/huamei_ptjqan/afts/img/A*kF_GTppRbp4AAAAAAAAAAAAADkN6AQ/original'
            }), /*#__PURE__*/ React.createElement("div", {
                style: {
                    overflow: 'hidden',
                    textOverflow: 'ellipsis',
                    WebkitBoxOrient: 'vertical',
                    WebkitLineClamp: 2,
                    display: '-webkit-box'
                }
            }, (item === null || item === void 0 ? void 0 : (_item_docMeta2 = item.docMeta) === null || _item_docMeta2 === void 0 ? void 0 : _item_docMeta2.doc_name) || (item === null || item === void 0 ? void 0 : item.docMeta.answer))) : null)
        }, props.render ? (_props_render = props.render) === null || _props_render === void 0 ? void 0 : _props_render.call(props, item, dom) : dom);
    })))));
};
