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 _instanceof(left, right) {
    if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
        return !!right[Symbol.hasInstance](left);
    } else {
        return left instanceof right;
    }
}
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 _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 classNames from "clsx";
import { useMergedState } from "rc-util";
import React, { memo, useMemo } from "react";
import { useRefFunction } from "../../Hooks/useRefFunction";
import { ToolContent, ToolExpand, ToolHeaderRight, ToolImage, ToolTime } from "./Content";
var ToolUseBarItemComponent = function ToolUseBarItemComponent(param) {
    var tool = param.tool, prefixCls = param.prefixCls, hashId = param.hashId, onClick = param.onClick, isActive = param.isActive, onActiveChange = param.onActiveChange, isExpanded = param.isExpanded, onExpandedChange = param.onExpandedChange, defaultExpanded = param.defaultExpanded, _param_light = param.light, light = _param_light === void 0 ? false : _param_light, _param_disableAnimation = param.disableAnimation, disableAnimation = _param_disableAnimation === void 0 ? false : _param_disableAnimation;
    // 使用 useMergedState 来管理展开状态
    var _useMergedState = _sliced_to_array(useMergedState(defaultExpanded !== null && defaultExpanded !== void 0 ? defaultExpanded : false, {
        value: isExpanded,
        onChange: onExpandedChange ? function(value) {
            return onExpandedChange(tool.id, value);
        } : undefined
    }), 2), expanded = _useMergedState[0], setExpanded = _useMergedState[1];
    var showContent = useMemo(function() {
        return !!(tool.status === 'error' && tool.errorMessage) || !!tool.content;
    }, [
        tool.status,
        tool.errorMessage,
        tool.content
    ]);
    // 使用 useMemo 优化样式类名
    var toolClassName = useMemo(function() {
        var _obj;
        return classNames("".concat(prefixCls, "-tool ").concat(hashId), (_obj = {}, _define_property(_obj, "".concat(prefixCls, "-tool-success"), tool.status === 'success'), _define_property(_obj, "".concat(prefixCls, "-tool-loading"), tool.status === 'loading'), _define_property(_obj, "".concat(prefixCls, "-tool-loading-light"), tool.status === 'loading' && light), _define_property(_obj, "".concat(prefixCls, "-tool-error"), tool.status === 'error'), _define_property(_obj, "".concat(prefixCls, "-tool-idle"), tool.status === 'idle'), _define_property(_obj, "".concat(prefixCls, "-tool-active"), isActive && !expanded), _define_property(_obj, "".concat(prefixCls, "-tool-expanded"), expanded), _define_property(_obj, "".concat(prefixCls, "-tool-light"), light), _obj));
    }, [
        prefixCls,
        hashId,
        tool.status,
        light,
        isActive,
        expanded
    ]);
    var toolBarClassName = useMemo(function() {
        return classNames("".concat(prefixCls, "-tool-bar"), hashId);
    }, [
        prefixCls,
        hashId
    ]);
    var toolHeaderClassName = useMemo(function() {
        return classNames("".concat(prefixCls, "-tool-header"), hashId);
    }, [
        prefixCls,
        hashId
    ]);
    var handleClick = useRefFunction(function(e) {
        onClick === null || onClick === void 0 ? void 0 : onClick(tool.id);
        if (onActiveChange && !showContent) {
            onActiveChange(tool.id, !isActive);
        }
        // 如果没有内容需要展示，则早返回
        if (!showContent) return;
        // 避免在交互性子元素上误触发折叠
        if (_instanceof(e.target, Element)) {
            var tag = e.target.tagName.toLowerCase();
            if ([
                'a',
                'button',
                'input',
                'textarea',
                'select',
                'label'
            ].includes(tag)) return;
        }
        // 使用函数式更新避免闭包陈旧值问题
        setExpanded(function(prev) {
            return !prev;
        });
    });
    var handleExpandClick = useRefFunction(function(e) {
        e.stopPropagation(); // 阻止事件冒泡到父元素
        setExpanded(function(prev) {
            return !prev;
        });
    });
    if (tool.type === 'summary') {
        return /*#__PURE__*/ React.createElement(ToolContent, {
            tool: tool,
            prefixCls: prefixCls,
            hashId: hashId,
            light: light,
            showContent: true,
            expanded: true,
            disableAnimation: disableAnimation
        });
    }
    return /*#__PURE__*/ React.createElement("div", {
        key: tool.id,
        "data-testid": "ToolUserItem",
        className: classNames(toolClassName, _define_property({}, "".concat(prefixCls, "-tool-collapsed"), !showContent))
    }, /*#__PURE__*/ React.createElement("div", {
        className: classNames(toolBarClassName, _define_property({}, "".concat(prefixCls, "-tool-bar-collapsed"), !showContent)),
        "data-testid": "tool-user-item-tool-bar",
        onClick: handleClick
    }, /*#__PURE__*/ React.createElement("div", {
        className: toolHeaderClassName,
        "data-testid": "tool-user-item-tool-header"
    }, /*#__PURE__*/ React.createElement(ToolImage, {
        tool: tool,
        prefixCls: prefixCls,
        hashId: hashId,
        disableAnimation: disableAnimation
    })), /*#__PURE__*/ React.createElement(ToolHeaderRight, {
        tool: tool,
        prefixCls: prefixCls,
        hashId: hashId,
        light: light,
        disableAnimation: disableAnimation
    }), tool.time || showContent ? /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(prefixCls, "-tool-time-expand"), hashId)
    }, /*#__PURE__*/ React.createElement(ToolTime, {
        tool: tool,
        prefixCls: prefixCls,
        hashId: hashId
    }), /*#__PURE__*/ React.createElement(ToolExpand, {
        showContent: showContent,
        expanded: expanded,
        prefixCls: prefixCls,
        hashId: hashId,
        onExpandClick: handleExpandClick,
        disableAnimation: disableAnimation
    })) : null), /*#__PURE__*/ React.createElement(ToolContent, {
        tool: tool,
        prefixCls: prefixCls,
        hashId: hashId,
        light: light,
        showContent: showContent,
        expanded: expanded,
        disableAnimation: disableAnimation
    }));
};
// 使用 memo 优化组件，避免不必要的重新渲染
export var ToolUseBarItem = /*#__PURE__*/ memo(ToolUseBarItemComponent);
