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 { ConfigProvider } from "antd";
import classNames from "clsx";
import React, { memo, useContext, useEffect, useState } from "react";
import { useRefFunction } from "../Hooks/useRefFunction";
import ButtonTab from "./ButtonTab";
import { useStyle } from "./ButtonTabGroupStyle";
var EMPTY_ITEMS = [];
var ButtonTabGroupComponent = function ButtonTabGroupComponent(param) {
    var _param_items = param.items, items = _param_items === void 0 ? EMPTY_ITEMS : _param_items, activeKey = param.activeKey, defaultActiveKey = param.defaultActiveKey, onChange = param.onChange, className = param.className, customPrefixCls = param.prefixCls;
    var getPrefixCls = useContext(ConfigProvider.ConfigContext).getPrefixCls;
    // P0-4：与 ButtonTab 一同迁入 'agentic-chatboot-*' 命名空间，并接入 ConfigProvider。
    var prefixCls = getPrefixCls('agentic-chatboot-button-tab-group', customPrefixCls);
    var _useStyle = useStyle(prefixCls), wrapSSR = _useStyle.wrapSSR, hashId = _useStyle.hashId;
    // P0-1：受控判定一次确定，避免后续读取 props 时状态漂移
    var isControlled = activeKey !== undefined;
    var _useState = _sliced_to_array(useState(function() {
        var _items_;
        return defaultActiveKey !== null && defaultActiveKey !== void 0 ? defaultActiveKey : (_items_ = items[0]) === null || _items_ === void 0 ? void 0 : _items_.key;
    }), 2), internalActiveKey = _useState[0], setInternalActiveKey = _useState[1];
    // P0-1：非受控模式下，items 异步加载或动态变更时，若当前选中项不在新 items 中，
    // 回落到第一个有效项；这样首屏 items=[] 后 items 到达时也能默认选中第一项。
    useEffect(function() {
        var _items_;
        if (isControlled) return;
        if (internalActiveKey && items.some(function(it) {
            return it.key === internalActiveKey;
        })) {
            return;
        }
        setInternalActiveKey((_items_ = items[0]) === null || _items_ === void 0 ? void 0 : _items_.key);
    }, [
        items,
        isControlled,
        internalActiveKey
    ]);
    var currentActiveKey = isControlled ? activeKey : internalActiveKey;
    var handleTabClick = useRefFunction(function(key, disabled) {
        if (disabled) return;
        if (!isControlled) {
            setInternalActiveKey(key);
        }
        onChange === null || onChange === void 0 ? void 0 : onChange(key);
    });
    return wrapSSR(// P0-2：用 clsx 拼接 className，避免模板字符串产生连续空格与命名风格不一致
    // P1-9：testid 与 prefixCls 解耦
    /*#__PURE__*/ React.createElement("div", {
        className: classNames(prefixCls, className, hashId),
        "data-testid": "agentic-chatboot-button-tab-group",
        role: "group",
        "aria-label": "Tab group"
    }, items.map(function(item) {
        return /*#__PURE__*/ React.createElement(ButtonTab, {
            key: item.key,
            selected: currentActiveKey === item.key,
            // P0-3：disabled 显式下传，由 ButtonTab 走原生 disabled / aria-disabled / 键盘拦截
            disabled: item.disabled,
            onClick: function onClick() {
                return handleTabClick(item.key, item.disabled);
            },
            onIconClick: item.onIconClick,
            icon: item.icon
        }, item.label);
    })));
};
ButtonTabGroupComponent.displayName = 'ButtonTabGroup';
// 使用 React.memo 优化性能，避免不必要的重新渲染
var ButtonTabGroup = /*#__PURE__*/ memo(ButtonTabGroupComponent);
export default ButtonTabGroup;
