import { useCallback, useMemo, useState } from 'react';
import { equals, filter, remove } from 'ramda';
import { getTableRowsData, getIsRowCollapsibleFromMaxValuesCount } from './dataHelpers';
import { useDidUpdateEffect } from '../useDidUpdateEffect';
import { usePrevious } from '../../hooks/usePrevious';
export var useCollapsibleTableRows = function (_a) {
    var rowsData = _a.rowsData, columnsData = _a.columnsData, renderRowCell = _a.renderRowCell, getRowCellHeight = _a.getRowCellHeight, maxRowValuesCount = _a.maxRowValuesCount, getIdFromRowValue = _a.getIdFromRowValue, applyMaxValuesCountConstraint = _a.applyMaxValuesCountConstraint;
    var _b = useState([]), expandedIndexes = _b[0], setExpandedIndexes = _b[1];
    var getRowMaxValuesCount = useCallback(function (rowIndex) {
        return expandedIndexes.includes(rowIndex) ? Infinity : maxRowValuesCount;
    }, [expandedIndexes, maxRowValuesCount]);
    var tableRowsData = useMemo(function () {
        return getTableRowsData(rowsData, columnsData, renderRowCell, getRowCellHeight, getRowMaxValuesCount, getIdFromRowValue, applyMaxValuesCountConstraint);
    }, [
        rowsData,
        columnsData,
        renderRowCell,
        getRowCellHeight,
        getRowMaxValuesCount,
        getIdFromRowValue,
        applyMaxValuesCountConstraint
    ]);
    var toggleRowCollapse = useCallback(function (rowIndex) {
        setExpandedIndexes(function (expandedIndexes) {
            var index = expandedIndexes.findIndex(equals(rowIndex));
            return index === -1 ? expandedIndexes.concat(rowIndex) : remove(index, 1, expandedIndexes);
        });
    }, []);
    var getIsRowCollapsible = useCallback(function (rowValue) {
        return getIsRowCollapsibleFromMaxValuesCount(columnsData, rowValue, maxRowValuesCount, applyMaxValuesCountConstraint);
    }, [maxRowValuesCount, columnsData, applyMaxValuesCountConstraint]);
    var getIsRowCollapsed = useCallback(function (rowIndex) {
        return !expandedIndexes.includes(rowIndex);
    }, [expandedIndexes]);
    var collapseContextValue = useMemo(function () { return ({
        maxRowValuesCount: maxRowValuesCount,
        toggleRowCollapse: toggleRowCollapse,
        getIsRowCollapsible: getIsRowCollapsible,
        getIsRowCollapsed: getIsRowCollapsed
    }); }, [maxRowValuesCount, toggleRowCollapse, getIsRowCollapsible, getIsRowCollapsed]);
    var prevRowsData = usePrevious(rowsData);
    useDidUpdateEffect(function () {
        if (expandedIndexes.length > 0) {
            setExpandedIndexes(filter(function (index) {
                var _a, _b;
                var rowValue = (_a = rowsData[index]) !== null && _a !== void 0 ? _a : {};
                var prevRowValue = (_b = prevRowsData[index]) !== null && _b !== void 0 ? _b : {};
                return rowValue.rawValue === prevRowValue.rawValue && getIsRowCollapsible(rowValue);
            }));
        }
    }, [rowsData]);
    return { tableRowsData: tableRowsData, collapseContextValue: collapseContextValue };
};
