"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VirtualizedCardList = void 0;
const react_1 = __importDefault(require("react"));
const classnames_1 = __importDefault(require("classnames"));
const cn_ui_1 = require("@cainiaofe/cn-ui");
const react_virtual_1 = require("../../../../../cn-ai-table/react-virtual");
const primary_key_1 = require("../../../../../cn-ai-table/view/utils/primary-key");
require("./virtualized-card-list.scss");
/**
 * 虚拟滚动卡片列表组件
 * 支持大量数据的高性能渲染
 * 使用统一的公共类名，支持通过 className 传递自定义样式
 */
const VirtualizedCardList = ({ data, renderCardContent, renderCardTitle, showFieldTitle, selectedIndex, onItemClick, visibleFieldCount, primaryKey, className, }) => {
    const scrollContainerRef = react_1.default.useRef(null);
    // 根据字段数量和是否显示标题估算卡片高度
    const estimatedCardHeight = react_1.default.useMemo(() => {
        const base = showFieldTitle ? 140 : 120;
        const perField = 28;
        return base + visibleFieldCount * perField;
    }, [showFieldTitle, visibleFieldCount]);
    const virtualizer = (0, react_virtual_1.useVirtualizer)({
        count: data.length,
        getScrollElement: () => scrollContainerRef.current,
        estimateSize: () => estimatedCardHeight,
        overscan: 5,
    });
    // 当选中项变化时，滚动到选中项
    react_1.default.useEffect(() => {
        if (selectedIndex >= 0 && selectedIndex < data.length) {
            virtualizer.scrollToIndex(selectedIndex, {
                align: 'center',
                behavior: 'smooth',
            });
        }
    }, [selectedIndex, data.length, virtualizer]);
    const virtualItems = virtualizer.getVirtualItems();
    const paddingTop = virtualItems.length > 0 ? virtualItems[0].start : 0;
    const paddingBottom = virtualItems.length > 0
        ? virtualizer.getTotalSize() - virtualItems[virtualItems.length - 1].end
        : 0;
    const virtualizedData = virtualItems
        .map((virtualItem) => ({
        virtualItem,
        record: data[virtualItem.index],
        index: virtualItem.index,
    }))
        .filter((item) => item.record);
    return (react_1.default.createElement("div", { className: (0, classnames_1.default)('card-list-scroll', className), ref: scrollContainerRef },
        react_1.default.createElement("div", { style: { height: paddingTop } }),
        react_1.default.createElement(cn_ui_1.CnListPro, { shape: "card", className: "card-list-grid", grid: {
                column: 1,
                wrap: true,
            }, dataSource: virtualizedData, renderItem: (item) => {
                const { virtualItem, record, index } = item;
                if (!record) {
                    return null;
                }
                const isSelected = index === selectedIndex;
                const rowKey = (0, primary_key_1.safeGetRowKey)(record, primaryKey, virtualItem.index);
                return (react_1.default.createElement("div", { key: rowKey, "data-index": virtualItem.index, ref: virtualizer.measureElement },
                    react_1.default.createElement(cn_ui_1.CnListProItem, { className: (0, classnames_1.default)('card-item', {
                            'card-item--selected': isSelected,
                        }), onClick: () => onItemClick === null || onItemClick === void 0 ? void 0 : onItemClick(record, index), style: { cursor: 'pointer' }, title: renderCardTitle(record, virtualItem.index), description: renderCardContent(record, virtualItem.index) })));
            } }),
        react_1.default.createElement("div", { style: { height: paddingBottom } })));
};
exports.VirtualizedCardList = VirtualizedCardList;
exports.default = exports.VirtualizedCardList;
