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 { Api, ChevronUp, ChevronsDownUp, ChevronsUpDown, X } from "@sofa-design/icons";
import classNames from "clsx";
import { motion } from "framer-motion";
import React, { memo, useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useRefFunction } from "../../Hooks/useRefFunction";
/** 内容超出此高度时自动收起 */ var CONTENT_COLLAPSE_THRESHOLD = 200;
/** 工具详情收起动画时长（毫秒） */ var TOOL_CONTENT_COLLAPSE_DURATION_MS = 160;
var ToolImageComponent = function ToolImageComponent(param) {
    var tool = param.tool, prefixCls = param.prefixCls, hashId = param.hashId, _param_disableAnimation = param.disableAnimation, disableAnimation = _param_disableAnimation === void 0 ? false : _param_disableAnimation;
    var toolImageWrapperClassName = useMemo(function() {
        var _obj;
        return classNames("".concat(prefixCls, "-tool-image-wrapper"), hashId, (_obj = {}, _define_property(_obj, "".concat(prefixCls, "-tool-image-wrapper-rotating"), tool.status === 'loading'), _define_property(_obj, "".concat(prefixCls, "-tool-image-wrapper-loading"), tool.status === 'loading'), _obj));
    }, [
        prefixCls,
        hashId,
        tool.status
    ]);
    var toolImageClassName = useMemo(function() {
        return classNames("".concat(prefixCls, "-tool-image"), hashId);
    }, [
        prefixCls,
        hashId
    ]);
    // 缓存动画配置，避免重复创建对象
    var loadingAnimationConfig = useMemo(function() {
        return {
            animate: {
                '--rotate': [
                    '0deg',
                    '360deg'
                ]
            },
            transition: {
                '--rotate': {
                    duration: 1,
                    repeat: Infinity,
                    ease: 'linear'
                }
            },
            style: {
                '--rotation': '360deg'
            }
        };
    }, []);
    var idleAnimationConfig = useMemo(function() {
        return {
            style: {
                '--rotation': '0deg'
            }
        };
    }, []);
    var animationProps = useMemo(function() {
        if (disableAnimation) return {};
        return tool.status === 'loading' ? loadingAnimationConfig : idleAnimationConfig;
    }, [
        tool.status,
        loadingAnimationConfig,
        idleAnimationConfig,
        disableAnimation
    ]);
    // 缓存图标渲染
    var iconElement = useMemo(function() {
        return tool.icon ? tool.icon : /*#__PURE__*/ React.createElement(Api, null);
    }, [
        tool.icon
    ]);
    if (disableAnimation) {
        return /*#__PURE__*/ React.createElement("div", {
            className: toolImageWrapperClassName
        }, /*#__PURE__*/ React.createElement("div", {
            className: toolImageClassName
        }, iconElement));
    }
    return /*#__PURE__*/ React.createElement(motion.div, _object_spread({
        className: toolImageWrapperClassName
    }, animationProps), /*#__PURE__*/ React.createElement("div", {
        className: toolImageClassName
    }, iconElement));
};
export var ToolImage = /*#__PURE__*/ memo(ToolImageComponent);
var ToolHeaderRightComponent = function ToolHeaderRightComponent(param) {
    var tool = param.tool, prefixCls = param.prefixCls, hashId = param.hashId, light = param.light, _param_disableAnimation = param.disableAnimation, disableAnimation = _param_disableAnimation === void 0 ? false : _param_disableAnimation;
    var toolHeaderRightClassName = useMemo(function() {
        return classNames("".concat(prefixCls, "-tool-header-right"), _define_property({}, "".concat(prefixCls, "-tool-header-right-light"), light), hashId);
    }, [
        prefixCls,
        hashId,
        light
    ]);
    var toolNameClassName = useMemo(function() {
        return classNames("".concat(prefixCls, "-tool-name"), hashId, _define_property({}, "".concat(prefixCls, "-tool-name-loading"), tool.status === 'loading'));
    }, [
        prefixCls,
        hashId,
        tool.status
    ]);
    var toolTargetClassName = useMemo(function() {
        var _obj;
        return classNames("".concat(prefixCls, "-tool-target"), hashId, (_obj = {}, _define_property(_obj, "".concat(prefixCls, "-tool-target-loading"), tool.status === 'loading'), _define_property(_obj, "".concat(prefixCls, "-tool-target-light"), light), _obj));
    }, [
        prefixCls,
        hashId,
        tool.status,
        light
    ]);
    // 缓存加载动画配置
    var loadingAnimationConfig = useMemo(function() {
        return {
            animate: {
                maskImage: [
                    'linear-gradient(to right, rgba(0,0,0,0.99)  -50%, rgba(0,0,0,0.15)   -50%,rgba(0,0,0,0.99)  150%)',
                    'linear-gradient(to right, rgba(0,0,0,0.99)  -50%,  rgba(0,0,0,0.15)  150%,rgba(0,0,0,0.99)  150%)'
                ]
            },
            transition: {
                maskImage: {
                    duration: 1,
                    repeat: Infinity,
                    ease: 'linear'
                }
            },
            style: {
                maskImage: 'linear-gradient(to right, rgba(0,0,0,0.99) -30%, rgba(0,0,0,0.15) -50%, rgba(0,0,0,0.99) 120%)'
            }
        };
    }, []);
    var animationProps = useMemo(function() {
        if (disableAnimation) return {};
        return tool.status === 'loading' ? loadingAnimationConfig : {};
    }, [
        tool.status,
        loadingAnimationConfig,
        disableAnimation
    ]);
    // 缓存工具名称和目标渲染
    var toolNameElement = useMemo(function() {
        return tool.toolName ? /*#__PURE__*/ React.createElement("div", {
            className: toolNameClassName
        }, tool.toolName) : null;
    }, [
        tool.toolName,
        toolNameClassName
    ]);
    var toolTargetElement = useMemo(function() {
        var _ref;
        var _tool_toolTarget;
        return tool.toolTarget ? /*#__PURE__*/ React.createElement("div", {
            className: toolTargetClassName,
            title: (_ref = (_tool_toolTarget = tool.toolTarget) === null || _tool_toolTarget === void 0 ? void 0 : _tool_toolTarget.toString()) !== null && _ref !== void 0 ? _ref : undefined
        }, tool.toolTarget) : null;
    }, [
        tool.toolTarget,
        toolTargetClassName
    ]);
    if (disableAnimation) {
        return /*#__PURE__*/ React.createElement("div", {
            className: toolHeaderRightClassName
        }, toolNameElement, toolTargetElement);
    }
    return /*#__PURE__*/ React.createElement(motion.div, _object_spread({
        className: toolHeaderRightClassName
    }, animationProps), toolNameElement, toolTargetElement);
};
export var ToolHeaderRight = /*#__PURE__*/ memo(ToolHeaderRightComponent);
var ToolTimeComponent = function ToolTimeComponent(param) {
    var tool = param.tool, prefixCls = param.prefixCls, hashId = param.hashId;
    var toolTimeClassName = useMemo(function() {
        return classNames("".concat(prefixCls, "-tool-time"), hashId);
    }, [
        prefixCls,
        hashId
    ]);
    // 缓存时间元素渲染
    var timeElement = useMemo(function() {
        return tool.time ? /*#__PURE__*/ React.createElement("div", {
            className: toolTimeClassName
        }, tool.time) : null;
    }, [
        tool.time,
        toolTimeClassName
    ]);
    return timeElement;
};
export var ToolTime = /*#__PURE__*/ memo(ToolTimeComponent);
var ToolExpandComponent = function ToolExpandComponent(param) {
    var showContent = param.showContent, expanded = param.expanded, prefixCls = param.prefixCls, hashId = param.hashId, onExpandClick = param.onExpandClick, _param_disableAnimation = param.disableAnimation, disableAnimation = _param_disableAnimation === void 0 ? false : _param_disableAnimation;
    var toolExpandClassName = useMemo(function() {
        return classNames("".concat(prefixCls, "-tool-expand"), hashId);
    }, [
        prefixCls,
        hashId
    ]);
    // 缓存样式对象，避免重复创建
    var chevronStyle = useMemo(function() {
        return _object_spread_props(_object_spread({}, disableAnimation ? {} : {
            transition: 'transform 0.3s ease-in-out'
        }), {
            transform: expanded ? 'rotate(180deg)' : 'rotate(0deg)'
        });
    }, [
        expanded,
        disableAnimation
    ]);
    // 使用 useRefFunction 优化点击处理函数
    var handleClick = useRefFunction(function(e) {
        onExpandClick(e);
    });
    // 缓存展开按钮元素
    var expandElement = useMemo(function() {
        if (!showContent) return null;
        return /*#__PURE__*/ React.createElement("div", {
            className: toolExpandClassName,
            onClick: handleClick
        }, /*#__PURE__*/ React.createElement(ChevronUp, {
            style: chevronStyle
        }));
    }, [
        showContent,
        toolExpandClassName,
        handleClick,
        chevronStyle
    ]);
    return expandElement;
};
export var ToolExpand = /*#__PURE__*/ memo(ToolExpandComponent);
var ToolContentComponent = function ToolContentComponent(param) {
    var tool = param.tool, prefixCls = param.prefixCls, hashId = param.hashId, light = param.light, showContent = param.showContent, expanded = param.expanded, _param_disableAnimation = param.disableAnimation, disableAnimation = _param_disableAnimation === void 0 ? false : _param_disableAnimation;
    var contentInnerRef = useRef(null);
    var _useState = _sliced_to_array(useState(false), 2), isContentOverflowing = _useState[0], setIsContentOverflowing = _useState[1];
    var _useState1 = _sliced_to_array(useState(false), 2), contentExpanded = _useState1[0], setContentExpanded = _useState1[1];
    var _useState2 = _sliced_to_array(useState(expanded), 2), shouldRenderContent = _useState2[0], setShouldRenderContent = _useState2[1];
    var toolContainerClassName = useMemo(function() {
        return classNames("".concat(prefixCls, "-tool-container"), hashId, _define_property({}, "".concat(prefixCls, "-tool-container-light"), light));
    }, [
        prefixCls,
        hashId,
        light
    ]);
    var contentExpandClassName = useMemo(function() {
        return classNames("".concat(prefixCls, "-tool-content-expand"), hashId);
    }, [
        prefixCls,
        hashId
    ]);
    // 缓存错误样式类名
    var errorClassName = useMemo(function() {
        return classNames("".concat(prefixCls, "-tool-content-error"), hashId);
    }, [
        prefixCls,
        hashId
    ]);
    var errorIconClassName = useMemo(function() {
        return classNames("".concat(prefixCls, "-tool-content-error-icon"), hashId);
    }, [
        prefixCls,
        hashId
    ]);
    var errorTextClassName = useMemo(function() {
        return classNames("".concat(prefixCls, "-tool-content-error-text"), hashId);
    }, [
        prefixCls,
        hashId
    ]);
    var contentClassName = useMemo(function() {
        return classNames("".concat(prefixCls, "-tool-content"), hashId);
    }, [
        prefixCls,
        hashId
    ]);
    var errorDom = useMemo(function() {
        return tool.status === 'error' && tool.errorMessage ? /*#__PURE__*/ React.createElement("div", {
            className: errorClassName
        }, /*#__PURE__*/ React.createElement("div", {
            className: errorIconClassName
        }, /*#__PURE__*/ React.createElement(X, null)), /*#__PURE__*/ React.createElement("div", {
            className: errorTextClassName
        }, tool.errorMessage)) : null;
    }, [
        tool.status,
        tool.errorMessage,
        errorClassName,
        errorIconClassName,
        errorTextClassName
    ]);
    var contentDom = useMemo(function() {
        return tool.content ? /*#__PURE__*/ React.createElement("div", {
            className: contentClassName
        }, tool.content) : null;
    }, [
        tool.content,
        contentClassName
    ]);
    var checkOverflow = useCallback(function() {
        var el = contentInnerRef.current;
        if (!el) return;
        var scrollHeight = el.scrollHeight;
        setIsContentOverflowing(scrollHeight > CONTENT_COLLAPSE_THRESHOLD);
    }, []);
    useEffect(function() {
        if (!showContent || !expanded) return;
        checkOverflow();
        var el = contentInnerRef.current;
        if (!el) return;
        var observer = new ResizeObserver(checkOverflow);
        observer.observe(el);
        return function() {
            return observer.disconnect();
        };
    }, [
        showContent,
        expanded,
        tool.content,
        tool.errorMessage,
        checkOverflow
    ]);
    useEffect(function() {
        if (!showContent) {
            setShouldRenderContent(false);
            return;
        }
        if (expanded) {
            setShouldRenderContent(true);
            return;
        }
        var timer = window.setTimeout(function() {
            setShouldRenderContent(false);
        }, TOOL_CONTENT_COLLAPSE_DURATION_MS);
        return function() {
            window.clearTimeout(timer);
        };
    }, [
        expanded,
        showContent
    ]);
    var handleContentExpandToggle = useRefFunction(function() {
        setContentExpanded(function(prev) {
            return !prev;
        });
    });
    var showContentExpandButton = showContent && expanded && isContentOverflowing;
    var contentInnerStyle = useMemo(function() {
        if (!showContentExpandButton) return undefined;
        if (contentExpanded) return undefined;
        return {
            maxHeight: CONTENT_COLLAPSE_THRESHOLD,
            overflow: 'hidden'
        };
    }, [
        showContentExpandButton,
        contentExpanded
    ]);
    var contentExpandButton = useMemo(function() {
        if (!showContentExpandButton) return null;
        var expandIcon = contentExpanded ? /*#__PURE__*/ React.createElement(ChevronsDownUp, null) : /*#__PURE__*/ React.createElement(ChevronsUpDown, null);
        var expandText = contentExpanded ? '收起' : '展开';
        return /*#__PURE__*/ React.createElement("div", {
            className: contentExpandClassName,
            onClick: handleContentExpandToggle,
            "data-testid": "tool-content-expand",
            role: "button",
            tabIndex: 0,
            onKeyDown: function onKeyDown(e) {
                if (e.key === 'Enter' || e.key === ' ') {
                    e.preventDefault();
                    handleContentExpandToggle();
                }
            }
        }, expandIcon, expandText);
    }, [
        showContentExpandButton,
        contentExpanded,
        contentExpandClassName,
        handleContentExpandToggle
    ]);
    var innerContent = /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement("div", {
        ref: contentInnerRef,
        style: contentInnerStyle
    }, contentDom, errorDom), contentExpandButton);
    // 禁用动画时使用简单的显示/隐藏
    if (disableAnimation) {
        return showContent && expanded ? /*#__PURE__*/ React.createElement("div", {
            className: classNames(toolContainerClassName, _define_property({}, "".concat(prefixCls, "-tool-container-expanded"), true)),
            "data-testid": "tool-user-item-tool-container",
            style: {
                overflow: 'hidden'
            }
        }, innerContent) : null;
    }
    return /*#__PURE__*/ React.createElement(React.Fragment, null, showContent && shouldRenderContent ? /*#__PURE__*/ React.createElement("div", {
        className: classNames(toolContainerClassName, _define_property({}, "".concat(prefixCls, "-tool-container-expanded"), expanded)),
        "data-testid": "tool-user-item-tool-container",
        "aria-hidden": !expanded
    }, innerContent) : null, !showContent ? /*#__PURE__*/ React.createElement("div", {
        style: {
            overflow: 'hidden',
            height: 1,
            opacity: 0,
            visibility: 'hidden'
        },
        role: "presentation",
        "aria-hidden": "true"
    }, contentDom, errorDom) : null);
};
export var ToolContent = /*#__PURE__*/ memo(ToolContentComponent);
