"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useTableItems = useTableItems;
const react_1 = require("react");
const hooks_1 = require("../../../utils/hooks");
const collectTableRowEntries_1 = require("../helpers/collectTableRowEntries");
function useTableItems(args) {
    const { items, subRows, getRowId } = args;
    const { expandedRowIds, defaultExpandedRowIds, getRows, onExpandedRowIdsChange, isRowExpandable, } = subRows || {};
    const [nestedSubRowsExpandedIds, setNestedSubRowsExpandedIds] = (0, hooks_1.useControllableState)({
        value: expandedRowIds,
        defaultValue: defaultExpandedRowIds !== null && defaultExpandedRowIds !== void 0 ? defaultExpandedRowIds : [],
        onChange: onExpandedRowIdsChange,
    });
    const expandedIdsSet = (0, react_1.useMemo)(() => new Set(nestedSubRowsExpandedIds), [nestedSubRowsExpandedIds]);
    const { itemDetails, visibleItems, visibleRowIds, childRowIdsById } = (0, react_1.useMemo)(() => {
        const { itemDetails: rowEntriesMap, rootRowIds, childRowIdsById: _childRowIdsById, } = (0, collectTableRowEntries_1.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 = (0, react_1.useCallback)((id) => {
        setNestedSubRowsExpandedIds((prev) => prev.includes(id)
            ? prev.filter((expandedId) => expandedId !== id)
            : [...prev, id]);
    }, [setNestedSubRowsExpandedIds]);
    const isSubRowExpanded = (0, react_1.useCallback)((id) => expandedIdsSet.has(id), [expandedIdsSet]);
    return (0, react_1.useMemo)(() => ({
        items: visibleItems,
        itemDetails,
        visibleRowIds,
        childRowIdsById,
        onExpandedRowIdsChange: handleExpandedSubRowIdChange,
        isSubRowExpanded,
    }), [
        visibleItems,
        itemDetails,
        visibleRowIds,
        childRowIdsById,
        handleExpandedSubRowIdChange,
        isSubRowExpanded,
    ]);
}
//# sourceMappingURL=useTableItems.js.map