import { useCallback, useMemo } from "react";
import { useControllableState } from "../../../utils/hooks/index.js";
import { collectTableRowEntries, } from "../helpers/collectTableRowEntries.js";
function useTableItems(args) {
    const { items, subRows, getRowId } = args;
    const { expandedRowIds, defaultExpandedRowIds, getRows, onExpandedRowIdsChange, isRowExpandable, } = subRows || {};
    const [nestedSubRowsExpandedIds, setNestedSubRowsExpandedIds] = useControllableState({
        value: expandedRowIds,
        defaultValue: defaultExpandedRowIds !== null && defaultExpandedRowIds !== void 0 ? defaultExpandedRowIds : [],
        onChange: onExpandedRowIdsChange,
    });
    const expandedIdsSet = useMemo(() => new Set(nestedSubRowsExpandedIds), [nestedSubRowsExpandedIds]);
    const { itemDetails, visibleItems, visibleRowIds, childRowIdsById } = useMemo(() => {
        const { itemDetails: rowEntriesMap, rootRowIds, childRowIdsById: _childRowIdsById, } = collectTableRowEntries({
            items,
            getRowId,
            getRows,
            isRowExpandable,
        });
        const localVisibleItems = [];
        const localVisibleRowIds = [];
        const addVisibleRows = (rowId) => {
            const details = rowEntriesMap.get(rowId);
            if (!details) {
                return [];
            }
            localVisibleItems.push(details.rowData);
            localVisibleRowIds.push(details.id);
            const visibleDescendantRowIds = [];
            if (expandedIdsSet.has(details.id)) {
                for (const childRowId of details.children) {
                    const childVisibleRowIds = addVisibleRows(childRowId);
                    visibleDescendantRowIds.push(...childVisibleRowIds);
                }
            }
            return [details.id, ...visibleDescendantRowIds];
        };
        for (const rowId of rootRowIds) {
            addVisibleRows(rowId);
        }
        return {
            visibleItems: localVisibleItems,
            visibleRowIds: localVisibleRowIds,
            childRowIdsById: _childRowIdsById,
            itemDetails: rowEntriesMap,
        };
    }, [getRows, items, getRowId, isRowExpandable, expandedIdsSet]);
    const handleExpandedSubRowIdChange = useCallback((id) => {
        setNestedSubRowsExpandedIds((prev) => prev.includes(id)
            ? prev.filter((expandedId) => expandedId !== id)
            : [...prev, id]);
    }, [setNestedSubRowsExpandedIds]);
    const isSubRowExpanded = useCallback((id) => expandedIdsSet.has(id), [expandedIdsSet]);
    return useMemo(() => ({
        items: visibleItems,
        itemDetails,
        visibleRowIds,
        childRowIdsById,
        onExpandedRowIdsChange: handleExpandedSubRowIdChange,
        isSubRowExpanded,
    }), [
        visibleItems,
        itemDetails,
        visibleRowIds,
        childRowIdsById,
        handleExpandedSubRowIdChange,
        isSubRowExpanded,
    ]);
}
export { useTableItems };
//# sourceMappingURL=useTableItems.js.map