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_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 _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 _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 React from "react";
import { MIN_GROUP_SIZE } from "../constants";
import { formatTime, getItemTimestamp, 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 generateHistoryItems(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, formatTimeLocale = param.formatTimeLocale;
    var groupList = groupByCategory(filteredList || [], function(item) {
        if (groupBy) {
            return groupBy(item);
        }
        return formatTime(getItemTimestamp(item), formatTimeLocale);
    });
    // 计算每个分组的最大时间戳，用于跨分组排序。
    // 之前实现是 `Object.fromEntries(... Math.max(...list.map(...)))`：
    // 1) `list.map` 拷贝出一份新数组
    // 2) `Math.max(...spread)` 在 V8 里会把每个元素当独立参数压栈，长 list 下有栈帧消耗
    // 3) `Object.entries` + `Object.fromEntries` 又转两次 — 总共 O(3n)
    // 改成一次 reduce + 内部 for 循环，单遍 O(n) 不分配中间数组。
    var groupMaxTimes = {};
    for(var key in groupList){
        if (!Object.prototype.hasOwnProperty.call(groupList, key)) continue;
        var list = groupList[key];
        var max = 0;
        for(var i = 0; i < list.length; i += 1){
            var t = getItemTimestamp(list[i]);
            if (t > max) max = t;
        }
        groupMaxTimes[key] = max;
    }
    // 按照时间顺序对分组进行排序：今日 > 昨日 > 一周内 > 其他
    var sortedGroupKeys = Object.keys(groupList).sort(function(keyA, keyB) {
        return (groupMaxTimes[keyB] || 0) - (groupMaxTimes[keyA] || 0);
    });
    var buildItemNodes = function buildItemNodes(list) {
        return(// 用副本排序，避免就地修改 props 传入的数组从而污染外层 chatList state
        _to_consumable_array(list).sort(function(a, b) {
            if (sessionSort === false) return 0;
            if (sessionSort) {
                var result = sessionSort(a, b);
                return typeof result === 'boolean' ? 0 : result;
            }
            return getItemTimestamp(b) - getItemTimestamp(a);
        }).map(function(item) {
            return {
                key: item.sessionId || "item-".concat(item.id),
                type: 'item',
                onClick: function onClick1() {
                    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
                })
            };
        }));
    };
    var items = sortedGroupKeys.flatMap(function(key) {
        var list = groupList[key];
        // 少于最小数量时，直接平铺为普通条目，不展示分组标题
        if (list.length < MIN_GROUP_SIZE) {
            return buildItemNodes(list);
        }
        var firstItem = list.at(0);
        var label = customDateFormatter && (firstItem === null || firstItem === void 0 ? void 0 : firstItem.gmtCreate) ? customDateFormatter(firstItem.gmtCreate) : formatTime(getItemTimestamp(firstItem !== null && firstItem !== void 0 ? firstItem : {}), formatTimeLocale);
        return [
            {
                key: "group-".concat(key),
                type: 'group',
                label: groupLabelRender ? groupLabelRender(key, list, label) : label,
                children: buildItemNodes(list)
            }
        ];
    });
    return items;
};
