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 _array_without_holes(arr) {
    if (Array.isArray(arr)) return _array_like_to_array(arr);
}
function _iterable_to_array(iter) {
    if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
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 _non_iterable_spread() {
    throw new TypeError("Invalid attempt to spread 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 _to_consumable_array(arr) {
    return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
}
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 { ChevronUp, CircleDashed, SuccessFill, X } from "@sofa-design/icons";
import { ConfigProvider } from "antd";
import classNames from "clsx";
import { AnimatePresence, motion } from "framer-motion";
import { useMergedState } from "rc-util";
import React, { memo, useCallback, useContext, useMemo } from "react";
import { ActionIconBox } from "../Components/ActionIconBox";
import { Loading } from "../Components/Loading";
import { useRefFunction } from "../Hooks/useRefFunction";
import { I18nContext } from "../I18n";
import { useStyle } from "./style";
var LOADING_SIZE = 16;
var buildClassName = function buildClassName() {
    for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
        args[_key] = arguments[_key];
    }
    return classNames.apply(void 0, _to_consumable_array(args));
};
var hasTaskContent = function hasTaskContent(content) {
    if (Array.isArray(content)) {
        return content.length > 0;
    }
    return !!content;
};
var getArrowRotation = function getArrowRotation(collapsed) {
    return {
        transform: collapsed ? 'rotate(0deg)' : 'rotate(180deg)',
        transition: 'transform 0.3s ease'
    };
};
var StatusIcon = /*#__PURE__*/ memo(function(param) {
    var status = param.status, prefixCls = param.prefixCls, hashId = param.hashId;
    var statusMap = {
        success: /*#__PURE__*/ React.createElement(SuccessFill, null),
        loading: /*#__PURE__*/ React.createElement(Loading, {
            size: LOADING_SIZE
        }),
        pending: /*#__PURE__*/ React.createElement("div", {
            className: buildClassName("".concat(prefixCls, "-status-idle"), hashId)
        }, /*#__PURE__*/ React.createElement(CircleDashed, null)),
        error: /*#__PURE__*/ React.createElement(X, null)
    };
    return /*#__PURE__*/ React.createElement("div", {
        className: buildClassName("".concat(prefixCls, "-status"), "".concat(prefixCls, "-status-").concat(status), hashId),
        "data-testid": "task-list-status-".concat(status)
    }, statusMap[status]);
});
StatusIcon.displayName = 'StatusIcon';
var TaskListItem = /*#__PURE__*/ memo(function(param) {
    var item = param.item, isLast = param.isLast, prefixCls = param.prefixCls, hashId = param.hashId, expandedKeys = param.expandedKeys, onToggle = param.onToggle;
    var locale = useContext(I18nContext).locale;
    var isCollapsed = !expandedKeys.includes(item.key);
    var hasContent = hasTaskContent(item.content);
    // 使用 useCallback 优化切换处理函数
    var handleToggle = useCallback(function() {
        onToggle(item.key);
    }, [
        item.key,
        onToggle
    ]);
    var arrowTitle = isCollapsed ? (locale === null || locale === void 0 ? void 0 : locale['taskList.expand']) || '展开' : (locale === null || locale === void 0 ? void 0 : locale['taskList.collapse']) || '收起';
    var contentVariants = useMemo(function() {
        return {
            expanded: {
                height: 'auto',
                opacity: 1
            },
            collapsed: {
                height: 0,
                opacity: 0
            }
        };
    }, []);
    var contentTransition = useMemo(function() {
        return {
            height: {
                duration: 0.26,
                ease: [
                    0.4,
                    0,
                    0.2,
                    1
                ]
            },
            opacity: {
                duration: 0.2,
                ease: 'linear'
            }
        };
    }, []);
    return /*#__PURE__*/ React.createElement("div", {
        key: item.key,
        className: buildClassName("".concat(prefixCls, "-thoughtChainItem"), hashId),
        "data-testid": "task-list-thoughtChainItem"
    }, /*#__PURE__*/ React.createElement("div", {
        className: buildClassName("".concat(prefixCls, "-left"), hashId),
        onClick: handleToggle,
        "data-testid": "task-list-left"
    }, /*#__PURE__*/ React.createElement(StatusIcon, {
        status: item.status,
        prefixCls: prefixCls,
        hashId: hashId
    }), /*#__PURE__*/ React.createElement("div", {
        className: buildClassName("".concat(prefixCls, "-content-left"), hashId)
    }, !isLast && /*#__PURE__*/ React.createElement("div", {
        className: buildClassName("".concat(prefixCls, "-dash-line"), hashId),
        "data-testid": "task-list-dash-line"
    }))), /*#__PURE__*/ React.createElement("div", {
        className: buildClassName("".concat(prefixCls, "-right"), hashId)
    }, /*#__PURE__*/ React.createElement("div", {
        className: buildClassName("".concat(prefixCls, "-top"), hashId),
        onClick: handleToggle
    }, /*#__PURE__*/ React.createElement("div", {
        className: buildClassName("".concat(prefixCls, "-title"), hashId)
    }, item.title), hasContent && /*#__PURE__*/ React.createElement("div", {
        className: buildClassName("".concat(prefixCls, "-arrowContainer"), hashId),
        onClick: handleToggle,
        "data-testid": "task-list-arrowContainer"
    }, /*#__PURE__*/ React.createElement(ActionIconBox, {
        title: arrowTitle,
        iconStyle: getArrowRotation(isCollapsed),
        loading: false,
        onClick: handleToggle
    }, /*#__PURE__*/ React.createElement(ChevronUp, {
        "data-testid": "task-list-arrow"
    })))), /*#__PURE__*/ React.createElement(AnimatePresence, {
        initial: false
    }, !isCollapsed && /*#__PURE__*/ React.createElement(motion.div, {
        key: "task-content",
        variants: contentVariants,
        initial: "collapsed",
        animate: "expanded",
        exit: "collapsed",
        transition: contentTransition,
        className: buildClassName("".concat(prefixCls, "-body"), hashId)
    }, /*#__PURE__*/ React.createElement("div", {
        className: buildClassName("".concat(prefixCls, "-content"), hashId)
    }, item.content)))));
});
TaskListItem.displayName = 'TaskListItem';
var getDefaultExpandedKeys = function getDefaultExpandedKeys(items, isControlled) {
    return isControlled ? [] : items.map(function(item) {
        return item.key;
    });
};
/**
 * TaskList 组件
 *
 * 显示任务列表，支持展开/折叠、状态管理等功能
 * 支持受控和非受控两种模式
 *
 * @example
 * ```tsx
 * // 非受控模式
 * <TaskList
 *   items={[
 *     { key: 'task1', title: '任务1', content: '内容', status: 'success' }
 *   ]}
 * />
 *
 * // 受控模式
 * <TaskList
 *   items={tasks}
 *   expandedKeys={expandedKeys}
 *   onExpandedKeysChange={setExpandedKeys}
 * />
 * ```
 */ export var TaskList = /*#__PURE__*/ memo(function(param) {
    var items = param.items, className = param.className, expandedKeys = param.expandedKeys, onExpandedKeysChange = param.onExpandedKeysChange;
    var getPrefixCls = useContext(ConfigProvider.ConfigContext).getPrefixCls;
    var prefixCls = getPrefixCls('task-list');
    var _useStyle = useStyle(prefixCls), wrapSSR = _useStyle.wrapSSR, hashId = _useStyle.hashId;
    var isControlled = expandedKeys !== undefined;
    var defaultKeys = getDefaultExpandedKeys(items, isControlled);
    var _useMergedState = _sliced_to_array(useMergedState(defaultKeys, {
        value: expandedKeys,
        onChange: onExpandedKeysChange
    }), 2), internalExpandedKeys = _useMergedState[0], setInternalExpandedKeys = _useMergedState[1];
    var handleToggle = useRefFunction(function(key) {
        var currentExpanded = isControlled ? expandedKeys : internalExpandedKeys;
        var newExpandedKeys = currentExpanded.includes(key) ? currentExpanded.filter(function(k) {
            return k !== key;
        }) : _to_consumable_array(currentExpanded).concat([
            key
        ]);
        setInternalExpandedKeys(newExpandedKeys);
    });
    return wrapSSR(/*#__PURE__*/ React.createElement("div", {
        className: className
    }, items.map(function(item, index) {
        return /*#__PURE__*/ React.createElement(TaskListItem, {
            key: item.key,
            item: item,
            isLast: index === items.length - 1,
            prefixCls: prefixCls,
            hashId: hashId,
            expandedKeys: internalExpandedKeys,
            onToggle: handleToggle
        });
    })));
});
TaskList.displayName = 'TaskList';
