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 _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 { Brain, ChevronDown, ChevronsDownUp, ChevronsUpDown } from "@sofa-design/icons";
import { ConfigProvider } from "antd";
import classNames from "clsx";
import { useMergedState } from "rc-util";
import React, { memo, useContext, useEffect, useMemo, useRef, useState } from "react";
import { CARD_RESIZE_DURATION_MS, CARD_RESIZE_EASING } from "../Constants/cardResizeMotion";
import { useRefFunction } from "../Hooks/useRefFunction";
import { useLocale } from "../I18n";
import { useStyle } from "./style";
if (typeof CSS !== 'undefined' && CSS.registerProperty) {
    try {
        CSS.registerProperty({
            name: '--think-rotate',
            syntax: '<angle>',
            inherits: true,
            initialValue: '0deg'
        });
    } catch (unused) {
    // already registered
    }
}
var FLOATING_ICON_STYLE = {
    fontSize: 16,
    color: 'var(--color-gray-text-light)'
};
var CONTENT_COLLAPSE_THRESHOLD = 200;
var CHEVRON_EXPANDED = {
    transform: 'rotate(0deg)',
    transition: "transform ".concat(CARD_RESIZE_DURATION_MS, "ms ").concat(CARD_RESIZE_EASING)
};
var CHEVRON_COLLAPSED = {
    transform: 'rotate(-90deg)',
    transition: "transform ".concat(CARD_RESIZE_DURATION_MS, "ms ").concat(CARD_RESIZE_EASING)
};
var CONTENT_CLAMPED_STYLE = {
    maxHeight: CONTENT_COLLAPSE_THRESHOLD,
    overflow: 'hidden'
};
var CONTAINER_STYLE = {
    overflow: 'hidden'
};
var ToolUseBarThinkComponent = function ToolUseBarThinkComponent(param) {
    var toolName = param.toolName, toolTarget = param.toolTarget, time = param.time, icon = param.icon, thinkContent = param.thinkContent, status = param.status, testId = param.testId, expanded = param.expanded, _param_defaultExpanded = param.defaultExpanded, defaultExpanded = _param_defaultExpanded === void 0 ? false : _param_defaultExpanded, onExpandedChange = param.onExpandedChange, floatingExpanded = param.floatingExpanded, _param_defaultFloatingExpanded = param.defaultFloatingExpanded, defaultFloatingExpanded = _param_defaultFloatingExpanded === void 0 ? false : _param_defaultFloatingExpanded, onFloatingExpandedChange = param.onFloatingExpandedChange, customClassNames = param.classNames, styles = param.styles, _param_light = param.light, light = _param_light === void 0 ? false : _param_light;
    var getPrefixCls = useContext(ConfigProvider.ConfigContext).getPrefixCls;
    var prefixCls = getPrefixCls('agentic-tool-use-bar-think');
    var _useStyle = useStyle(prefixCls), wrapSSR = _useStyle.wrapSSR, hashId = _useStyle.hashId;
    var locale = useLocale();
    var _useMergedState = _sliced_to_array(useMergedState(defaultExpanded, {
        value: expanded,
        onChange: onExpandedChange
    }), 2), expandedState = _useMergedState[0], setExpandedState = _useMergedState[1];
    var _useMergedState1 = _sliced_to_array(useMergedState(defaultFloatingExpanded, {
        value: floatingExpanded,
        onChange: onFloatingExpandedChange
    }), 2), floatingExpandedState = _useMergedState1[0], setFloatingExpandedState = _useMergedState1[1];
    var _useState = _sliced_to_array(useState(false), 2), hover = _useState[0], setHover = _useState[1];
    var onMouseMove = useRefFunction(function() {
        return setHover(true);
    });
    var onMouseLeave = useRefFunction(function() {
        return setHover(false);
    });
    var handleToggleExpand = useRefFunction(function() {
        setExpandedState(!expandedState);
    });
    var handleToggleFloatingExpand = useRefFunction(function() {
        setFloatingExpandedState(!floatingExpandedState);
    });
    var isLoading = status === 'loading';
    useEffect(function() {
        if (isLoading) {
            setExpandedState(true);
        }
    }, [
        isLoading,
        setExpandedState
    ]);
    // --- Container overflow detection ---
    // Only active when expanded AND not loading (overflow UI is hidden during loading)
    var contentInnerRef = useRef(null);
    var _useState1 = _sliced_to_array(useState(false), 2), isContentOverflowing = _useState1[0], setIsContentOverflowing = _useState1[1];
    var _useState2 = _sliced_to_array(useState(false), 2), contentExpanded = _useState2[0], setContentExpanded = _useState2[1];
    var needOverflowCheck = expandedState && !isLoading && !!thinkContent;
    var checkOverflow = useRefFunction(function() {
        var el = contentInnerRef.current;
        if (!el) return;
        setIsContentOverflowing(el.scrollHeight > CONTENT_COLLAPSE_THRESHOLD);
    });
    useEffect(function() {
        if (!needOverflowCheck) return;
        checkOverflow();
        var el = contentInnerRef.current;
        if (!el) return;
        var observer = new ResizeObserver(checkOverflow);
        observer.observe(el);
        return function() {
            return observer.disconnect();
        };
    }, [
        needOverflowCheck,
        checkOverflow
    ]);
    var handleContentExpandToggle = useRefFunction(function() {
        setContentExpanded(function(prev) {
            return !prev;
        });
    });
    var showContentExpand = needOverflowCheck && isContentOverflowing;
    var showFloatingExpand = isLoading && !light;
    var contentInnerStyle = useMemo(function() {
        return showContentExpand && !contentExpanded ? CONTENT_CLAMPED_STYLE : undefined;
    }, [
        showContentExpand,
        contentExpanded
    ]);
    // --- Class names (memoized) ---
    var cls = useMemo(function() {
        var _obj;
        var root = classNames(prefixCls, hashId, customClassNames === null || customClassNames === void 0 ? void 0 : customClassNames.root, (_obj = {}, _define_property(_obj, "".concat(prefixCls, "-expanded"), !expandedState), _define_property(_obj, "".concat(prefixCls, "-loading"), isLoading), _define_property(_obj, "".concat(prefixCls, "-active"), expandedState), _define_property(_obj, "".concat(prefixCls, "-success"), status === 'success'), _define_property(_obj, "".concat(prefixCls, "-light"), light), _obj));
        var bar = classNames("".concat(prefixCls, "-bar"), hashId, customClassNames === null || customClassNames === void 0 ? void 0 : customClassNames.bar);
        var header = classNames("".concat(prefixCls, "-header"), hashId, customClassNames === null || customClassNames === void 0 ? void 0 : customClassNames.header, _define_property({}, "".concat(prefixCls, "-header-light"), light));
        var imageWrapper = classNames("".concat(prefixCls, "-image-wrapper"), hashId, customClassNames === null || customClassNames === void 0 ? void 0 : customClassNames.imageWrapper, _define_property({}, "".concat(prefixCls, "-image-wrapper-loading"), isLoading));
        var headerRight = classNames("".concat(prefixCls, "-header-right"), hashId, _define_property({}, "".concat(prefixCls, "-header-right-loading"), isLoading));
        var name = classNames("".concat(prefixCls, "-name"), hashId, customClassNames === null || customClassNames === void 0 ? void 0 : customClassNames.name, _define_property({}, "".concat(prefixCls, "-name-light"), light));
        var _obj1;
        var container = classNames("".concat(prefixCls, "-container"), hashId, customClassNames === null || customClassNames === void 0 ? void 0 : customClassNames.container, (_obj1 = {}, _define_property(_obj1, "".concat(prefixCls, "-container-expanded"), expandedState), _define_property(_obj1, "".concat(prefixCls, "-container-loading"), isLoading && !floatingExpandedState), _define_property(_obj1, "".concat(prefixCls, "-container-light"), light), _define_property(_obj1, "".concat(prefixCls, "-container-floating-expanded"), floatingExpandedState), _obj1));
        var content = classNames("".concat(prefixCls, "-content"), hashId, customClassNames === null || customClassNames === void 0 ? void 0 : customClassNames.content, _define_property({}, "".concat(prefixCls, "-content-light"), light));
        var floatingExpand = classNames("".concat(prefixCls, "-floating-expand"), hashId, customClassNames === null || customClassNames === void 0 ? void 0 : customClassNames.floatingExpand);
        var target = classNames("".concat(prefixCls, "-target"), hashId, customClassNames === null || customClassNames === void 0 ? void 0 : customClassNames.target);
        var timeEl = classNames("".concat(prefixCls, "-time"), hashId, customClassNames === null || customClassNames === void 0 ? void 0 : customClassNames.time);
        var expand = classNames("".concat(prefixCls, "-expand"), hashId, customClassNames === null || customClassNames === void 0 ? void 0 : customClassNames.expand);
        var contentExpand = classNames("".concat(prefixCls, "-content-expand"), hashId);
        var lightIcon = classNames("".concat(prefixCls, "-header-left-icon"), "".concat(prefixCls, "-header-left-icon-light"), hashId);
        return {
            root: root,
            bar: bar,
            header: header,
            imageWrapper: imageWrapper,
            headerRight: headerRight,
            name: name,
            container: container,
            content: content,
            floatingExpand: floatingExpand,
            target: target,
            time: timeEl,
            expand: expand,
            contentExpand: contentExpand,
            lightIcon: lightIcon
        };
    }, [
        prefixCls,
        hashId,
        customClassNames,
        expandedState,
        isLoading,
        status,
        light,
        floatingExpandedState
    ]);
    return wrapSSR(/*#__PURE__*/ React.createElement("div", {
        "data-testid": testId || 'ToolUseBarThink',
        className: classNames(cls.root, _define_property({}, "".concat(prefixCls, "-root-think-collapsed"), Boolean(thinkContent) && !expandedState)),
        style: styles === null || styles === void 0 ? void 0 : styles.root
    }, /*#__PURE__*/ React.createElement("div", {
        className: cls.bar,
        "data-testid": "tool-use-bar-think-bar",
        style: styles === null || styles === void 0 ? void 0 : styles.bar,
        onClick: handleToggleExpand
    }, /*#__PURE__*/ React.createElement("div", {
        className: cls.header,
        "data-testid": "tool-use-bar-think-header",
        style: styles === null || styles === void 0 ? void 0 : styles.header,
        onMouseMove: onMouseMove,
        onMouseLeave: onMouseLeave
    }, light ? /*#__PURE__*/ React.createElement("div", {
        className: cls.lightIcon
    }, hover ? /*#__PURE__*/ React.createElement(ChevronDown, {
        style: expandedState ? CHEVRON_EXPANDED : CHEVRON_COLLAPSED
    }) : /*#__PURE__*/ React.createElement(Brain, null)) : /*#__PURE__*/ React.createElement("div", {
        className: cls.imageWrapper,
        style: styles === null || styles === void 0 ? void 0 : styles.imageWrapper
    }, icon || /*#__PURE__*/ React.createElement(Brain, null)), /*#__PURE__*/ React.createElement("div", {
        className: cls.headerRight
    }, toolName && /*#__PURE__*/ React.createElement("div", {
        className: cls.name,
        style: styles === null || styles === void 0 ? void 0 : styles.name
    }, toolName), toolTarget && /*#__PURE__*/ React.createElement("div", {
        className: cls.target,
        style: styles === null || styles === void 0 ? void 0 : styles.target
    }, toolTarget))), time && /*#__PURE__*/ React.createElement("div", {
        className: cls.time,
        style: styles === null || styles === void 0 ? void 0 : styles.time
    }, time), thinkContent && !light && /*#__PURE__*/ React.createElement("div", {
        className: cls.expand,
        onClick: handleToggleExpand,
        style: styles === null || styles === void 0 ? void 0 : styles.expand
    }, expandedState ? /*#__PURE__*/ React.createElement(ChevronsDownUp, null) : /*#__PURE__*/ React.createElement(ChevronsUpDown, null))), thinkContent && /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(prefixCls, "-think-collapse"), hashId, _define_property({}, "".concat(prefixCls, "-think-collapse-open"), expandedState))
    }, /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(prefixCls, "-think-collapse-inner"), hashId)
    }, /*#__PURE__*/ React.createElement("div", {
        className: cls.container,
        "data-testid": "tool-use-bar-think-container",
        style: CONTAINER_STYLE
    }, /*#__PURE__*/ React.createElement("div", {
        ref: contentInnerRef,
        style: contentInnerStyle
    }, /*#__PURE__*/ React.createElement("div", {
        className: cls.content,
        style: styles === null || styles === void 0 ? void 0 : styles.content
    }, thinkContent)), showContentExpand && /*#__PURE__*/ React.createElement("div", {
        className: cls.contentExpand,
        onClick: handleContentExpandToggle,
        "data-testid": "tool-use-bar-think-content-expand",
        role: "button",
        tabIndex: 0,
        onKeyDown: function onKeyDown(e) {
            if (e.key === 'Enter' || e.key === ' ') {
                e.preventDefault();
                handleContentExpandToggle();
            }
        }
    }, contentExpanded ? /*#__PURE__*/ React.createElement(ChevronsDownUp, null) : /*#__PURE__*/ React.createElement(ChevronsUpDown, null), contentExpanded ? locale.collapse : locale.expand), showFloatingExpand && /*#__PURE__*/ React.createElement("div", {
        className: cls.floatingExpand,
        onClick: handleToggleFloatingExpand,
        "data-testid": "tool-use-bar-think-floating-expand",
        style: styles === null || styles === void 0 ? void 0 : styles.floatingExpand
    }, floatingExpandedState ? /*#__PURE__*/ React.createElement(ChevronsDownUp, {
        style: FLOATING_ICON_STYLE
    }) : /*#__PURE__*/ React.createElement(ChevronsUpDown, {
        style: FLOATING_ICON_STYLE
    }), floatingExpandedState ? locale.collapse : locale.expand)))), thinkContent && !expandedState && showFloatingExpand && /*#__PURE__*/ React.createElement("div", {
        className: cls.floatingExpand,
        onClick: handleToggleFloatingExpand,
        "data-testid": "tool-use-bar-think-floating-expand",
        style: styles === null || styles === void 0 ? void 0 : styles.floatingExpand
    }, floatingExpandedState ? /*#__PURE__*/ React.createElement(ChevronsDownUp, {
        style: FLOATING_ICON_STYLE
    }) : /*#__PURE__*/ React.createElement(ChevronsUpDown, {
        style: FLOATING_ICON_STYLE
    }), floatingExpandedState ? locale.collapse : locale.expand)));
};
export var ToolUseBarThink = /*#__PURE__*/ memo(ToolUseBarThinkComponent);
