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 dayjs from "dayjs";
import React from "react";
import { formatTime, groupByCategory } from "../utils";
import { HistoryItem } from "./HistoryItem";
/**
 * 生成历史记录菜单项
 *
 * 该函数将历史记录数据转换为菜单项格式，支持分组、排序、自定义格式化等功能。
 *
 * @function
 * @description 生成历史记录菜单项，支持分组和排序
 * @param {HistoryListConfig} config - 配置参数
 * @param {HistoryDataType[]} config.filteredList - 过滤后的历史记录列表
 * @param {string[]} config.selectedIds - 选中的记录ID列表
 * @param {Function} config.onSelectionChange - 选择状态变化回调
 * @param {Function} config.onSelected - 选择记录回调
 * @param {Function} [config.onDeleteItem] - 删除记录回调
 * @param {Function} [config.onFavorite] - 收藏记录回调
 * @param {Object} [config.agent] - Agent配置
 * @param {Function} [config.extra] - 额外渲染函数
 * @param {Function} [config.customDateFormatter] - 自定义日期格式化函数
 * @param {Function} [config.groupBy] - 自定义分组函数
 * @param {Function|boolean} [config.sessionSort] - 自定义排序函数
 * @param {string[]} [config.runningId] - 正在运行的记录ID列表
 *
 * @example
 * ```tsx
 * const menuItems = generateHistoryItems({
 *   filteredList: historyData,
 *   selectedIds: selectedIds,
 *   onSelectionChange: handleSelectionChange,
 *   onSelected: handleSelected,
 *   onDeleteItem: handleDelete,
 *   onFavorite: handleFavorite,
 *   agent: { enabled: true },
 *   customDateFormatter: (date) => dayjs(date).format('YYYY-MM-DD'),
 *   runningId: ['task-1', 'task-2']
 * });
 * ```
 *
 * @returns {Array} 生成的菜单项数组
 *
 * @remarks
 * - 支持按日期自动分组
 * - 支持自定义分组逻辑
 * - 支持自定义排序规则
 * - 支持自定义日期格式化
 * - 集成HistoryItem组件渲染
 * - 支持多选和收藏功能
 * - 支持运行状态图标显示
 */ export var generateHistoryItems = function(param) {
    var filteredList = param.filteredList, selectedIds = param.selectedIds, onSelectionChange = param.onSelectionChange, onClick = param.onClick, onDeleteItem = param.onDeleteItem, agent = param.agent, onFavorite = param.onFavorite, extra = param.extra, customDateFormatter = param.customDateFormatter, groupBy = param.groupBy, sessionSort = param.sessionSort, groupLabelRender = param.groupLabelRender, type = param.type, runningId = param.runningId, customOperationExtra = param.customOperationExtra, itemDateFormatter = param.itemDateFormatter;
    var _Math;
    var groupList = groupByCategory(filteredList || [], function(item) {
        if (groupBy) {
            return groupBy(item);
        }
        return formatTime(item.gmtCreate);
    });
    var groupMaxTimes = Object.fromEntries(Object.entries(groupList).map(function(param) {
        var _param = _sliced_to_array(param, 2), key = _param[0], list = _param[1];
        return [
            key,
            (_Math = Math).max.apply(_Math, _to_consumable_array(list.map(function(item) {
                return dayjs(item.gmtCreate).valueOf();
            })))
        ];
    }));
    // 按照时间顺序对分组进行排序：今日 > 昨日 > 一周内 > 其他
    var sortedGroupKeys = Object.keys(groupList).sort(function(keyA, keyB) {
        return (groupMaxTimes[keyB] || 0) - (groupMaxTimes[keyA] || 0);
    });
    var items = sortedGroupKeys.map(function(key) {
        var _list_sort;
        var list = groupList[key];
        var firstItem = list === null || list === void 0 ? void 0 : list.at(0);
        var label = customDateFormatter && (firstItem === null || firstItem === void 0 ? void 0 : firstItem.gmtCreate) ? customDateFormatter(firstItem.gmtCreate) : formatTime(firstItem === null || firstItem === void 0 ? void 0 : firstItem.gmtCreate);
        return {
            key: "group-".concat(key),
            type: 'group',
            label: groupLabelRender ? groupLabelRender(key, list, label) : label,
            children: list === null || list === void 0 ? void 0 : (_list_sort = list.sort(function(a, b) {
                if (sessionSort === false) {
                    return 0;
                }
                if (sessionSort) {
                    var result = sessionSort(a, b);
                    return typeof result === 'boolean' ? 0 : result;
                }
                return dayjs(b.gmtCreate).valueOf() - dayjs(a.gmtCreate).valueOf();
            })) === null || _list_sort === void 0 ? void 0 : _list_sort.map(function(item) {
                return {
                    key: item.sessionId || "item-".concat(item.id),
                    type: 'item',
                    onClick: function() {
                        if (!item.sessionId) {
                            return;
                        }
                        onClick(item.sessionId, item);
                    },
                    label: /*#__PURE__*/ React.createElement(HistoryItem, {
                        item: item,
                        selectedIds: selectedIds,
                        onSelectionChange: onSelectionChange,
                        onClick: onClick,
                        onDeleteItem: onDeleteItem,
                        agent: agent,
                        onFavorite: onFavorite,
                        extra: extra,
                        type: type,
                        runningId: runningId,
                        customOperationExtra: customOperationExtra,
                        itemDateFormatter: itemDateFormatter
                    })
                };
            })
        };
    });
    return items;
};
