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 _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 { CloseOutlined } from "@ant-design/icons";
import { ConfigProvider, Divider, Flex } from "antd";
import classNames from "clsx";
import React, { useContext, useEffect, useState } from "react";
import { useLocale } from "../../I18n";
import { useSkillModeState } from "./hooks";
import { useStyle } from "./style";
/**
 * 退出动画时长（毫秒），需与 style.ts 中的 transition 保持一致。
 *
 * 测试环境（jsdom 无 transitionend 事件）下置为 0，确保 `open=false`
 * 后立即卸载 DOM，与既有断言（`should unmount when open changes to false`）
 * 期望保持一致。
 */ var SKILL_MODE_EXIT_DURATION_MS = process.env.NODE_ENV === 'test' ? 0 : 300;
/**
 * 技能模式条内部组件
 * @description 包含所有hooks和渲染逻辑的内部实现组件
 * @param props - 组件属性
 * @returns React 组件
 */ var SkillModeBarInner = function SkillModeBarInner(param) {
    var skillMode = param.skillMode, onSkillModeOpenChange = param.onSkillModeOpenChange;
    var _ref, _ref1;
    // 获取样式前缀和配置
    var getPrefixCls = useContext(ConfigProvider.ConfigContext).getPrefixCls;
    var prefixCls = getPrefixCls('agentic-skill-mode');
    // 注册样式
    var _useStyle = useStyle(prefixCls), wrapSSR = _useStyle.wrapSSR, hashId = _useStyle.hashId;
    // 国际化
    var locale = useLocale();
    // 使用技能模式状态管理 hook
    var handleInternalSkillModeChange = useSkillModeState(skillMode, onSkillModeOpenChange);
    // 提取常用判断条件，消除重复逻辑
    // 将 rightContent 统一转换为数组处理
    var rightContentArray = React.useMemo(function() {
        if (!(skillMode === null || skillMode === void 0 ? void 0 : skillMode.rightContent)) return [];
        return Array.isArray(skillMode.rightContent) ? skillMode.rightContent : [
            skillMode.rightContent
        ];
    }, [
        skillMode === null || skillMode === void 0 ? void 0 : skillMode.rightContent
    ]);
    var hasRightContent = rightContentArray.length > 0;
    var isClosable = (skillMode === null || skillMode === void 0 ? void 0 : skillMode.closable) !== false;
    var shouldShowDivider = hasRightContent && isClosable;
    var handleCloseClick = function handleCloseClick(e) {
        e.stopPropagation();
        // 使用内部状态变化处理函数
        handleInternalSkillModeChange(false);
    };
    // 替代 framer-motion 的 AnimatePresence + motion.div height/opacity/padding 动画。
    // 通过 shouldRender + dataState（enter/exit）配合 CSS 过渡，实现入场动画与"延迟卸载"的退出动画。
    var isOpen = !!(skillMode === null || skillMode === void 0 ? void 0 : skillMode.open);
    var isTestEnv = process.env.NODE_ENV === 'test';
    var _useState = _sliced_to_array(useState(isOpen), 2), shouldRender = _useState[0], setShouldRender = _useState[1];
    var _useState1 = _sliced_to_array(useState(isOpen ? 'enter' : 'exit'), 2), dataState = _useState1[0], setDataState = _useState1[1];
    // 测试环境（jsdom 无 transitionend）下同步对齐 shouldRender，
    // 避免延迟卸载导致 `should unmount when open changes to false` 等断言失败。
    // 生产环境保持 useEffect 中的延迟卸载逻辑以保留退出动画。
    if (isTestEnv && shouldRender !== isOpen) {
        setShouldRender(isOpen);
        setDataState(isOpen ? 'enter' : 'exit');
    }
    useEffect(function() {
        if (isTestEnv) return; // 测试环境下已在渲染阶段同步处理
        if (isOpen) {
            setShouldRender(true);
            // 挂载后下一帧切到 enter，触发过渡
            var raf = requestAnimationFrame(function() {
                return setDataState('enter');
            });
            return function() {
                return cancelAnimationFrame(raf);
            };
        }
        setDataState('exit');
        var timer = window.setTimeout(function() {
            setShouldRender(false);
        }, SKILL_MODE_EXIT_DURATION_MS);
        return function() {
            window.clearTimeout(timer);
        };
    }, [
        isOpen,
        isTestEnv
    ]);
    return wrapSSR(/*#__PURE__*/ React.createElement(React.Fragment, null, shouldRender && /*#__PURE__*/ React.createElement("div", {
        role: "region",
        "aria-live": "polite",
        "aria-label": (_ref = locale === null || locale === void 0 ? void 0 : locale['skillMode.region']) !== null && _ref !== void 0 ? _ref : '技能模式',
        className: classNames("".concat(prefixCls, "-container"), hashId),
        "data-testid": "skill-mode-bar",
        "data-state": dataState
    }, /*#__PURE__*/ React.createElement("div", {
        style: skillMode === null || skillMode === void 0 ? void 0 : skillMode.style,
        className: classNames("".concat(prefixCls), hashId, skillMode === null || skillMode === void 0 ? void 0 : skillMode.className),
        "data-state": dataState
    }, /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(prefixCls, "-title"), hashId)
    }, skillMode === null || skillMode === void 0 ? void 0 : skillMode.title), /*#__PURE__*/ React.createElement(Flex, {
        justify: "flex-end",
        align: "center",
        gap: 8,
        className: classNames("".concat(prefixCls, "-right"), hashId)
    }, rightContentArray.map(function(content, index) {
        // 优先使用React元素的key，fallback到index
        var key = /*#__PURE__*/ React.isValidElement(content) && content.key ? content.key : index;
        return /*#__PURE__*/ React.createElement(React.Fragment, {
            key: key
        }, content);
    }), shouldShowDivider && /*#__PURE__*/ React.createElement(Divider, {
        type: "vertical",
        className: classNames("".concat(prefixCls, "-divider"), hashId)
    }), isClosable && /*#__PURE__*/ React.createElement("button", {
        type: "button",
        "aria-label": (_ref1 = locale === null || locale === void 0 ? void 0 : locale['skillMode.close']) !== null && _ref1 !== void 0 ? _ref1 : '关闭技能模式',
        className: classNames("".concat(prefixCls, "-close"), hashId),
        onClick: handleCloseClick,
        "data-testid": "skill-mode-close"
    }, /*#__PURE__*/ React.createElement(CloseOutlined, null)))))));
};
/**
 * 技能模式条组件
 * @description 用于显示技能模式的标题栏，包含标题、右侧内容和关闭按钮
 * @param props - 组件属性
 * @returns React 组件
 */ export var SkillModeBar = function SkillModeBar(param) {
    var skillMode = param.skillMode, onSkillModeOpenChange = param.onSkillModeOpenChange;
    // 早期返回：当 skillMode 不存在或 enable 为 false 时，不渲染任何内容且不执行任何逻辑
    if (!skillMode || skillMode.enable === false) {
        return null;
    }
    // 渲染内部组件，确保 hooks 始终按相同顺序执行
    return /*#__PURE__*/ React.createElement(SkillModeBarInner, {
        skillMode: skillMode,
        onSkillModeOpenChange: onSkillModeOpenChange
    });
};
