"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useCollapsibleTableRows = void 0;
var react_1 = require("react");
var ramda_1 = require("ramda");
var dataHelpers_1 = require("./dataHelpers");
var useDidUpdateEffect_1 = require("../useDidUpdateEffect");
var usePrevious_1 = require("../../hooks/usePrevious");
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 = (0, react_1.useState)([]), expandedIndexes = _b[0], setExpandedIndexes = _b[1];
    var getRowMaxValuesCount = (0, react_1.useCallback)(function (rowIndex) {
        return expandedIndexes.includes(rowIndex) ? Infinity : maxRowValuesCount;
    }, [expandedIndexes, maxRowValuesCount]);
    var tableRowsData = (0, react_1.useMemo)(function () {
        return (0, dataHelpers_1.getTableRowsData)(rowsData, columnsData, renderRowCell, getRowCellHeight, getRowMaxValuesCount, getIdFromRowValue, applyMaxValuesCountConstraint);
    }, [
        rowsData,
        columnsData,
        renderRowCell,
        getRowCellHeight,
        getRowMaxValuesCount,
        getIdFromRowValue,
        applyMaxValuesCountConstraint
    ]);
    var toggleRowCollapse = (0, react_1.useCallback)(function (rowIndex) {
        setExpandedIndexes(function (expandedIndexes) {
            var index = expandedIndexes.findIndex((0, ramda_1.equals)(rowIndex));
            return index === -1 ? expandedIndexes.concat(rowIndex) : (0, ramda_1.remove)(index, 1, expandedIndexes);
        });
    }, []);
    var getIsRowCollapsible = (0, react_1.useCallback)(function (rowValue) {
        return (0, dataHelpers_1.getIsRowCollapsibleFromMaxValuesCount)(columnsData, rowValue, maxRowValuesCount, applyMaxValuesCountConstraint);
    }, [maxRowValuesCount, columnsData, applyMaxValuesCountConstraint]);
    var getIsRowCollapsed = (0, react_1.useCallback)(function (rowIndex) {
        return !expandedIndexes.includes(rowIndex);
    }, [expandedIndexes]);
    var collapseContextValue = (0, react_1.useMemo)(function () { return ({
        maxRowValuesCount: maxRowValuesCount,
        toggleRowCollapse: toggleRowCollapse,
        getIsRowCollapsible: getIsRowCollapsible,
        getIsRowCollapsed: getIsRowCollapsed
    }); }, [maxRowValuesCount, toggleRowCollapse, getIsRowCollapsible, getIsRowCollapsed]);
    var prevRowsData = (0, usePrevious_1.usePrevious)(rowsData);
    (0, useDidUpdateEffect_1.useDidUpdateEffect)(function () {
        if (expandedIndexes.length > 0) {
            setExpandedIndexes((0, ramda_1.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 };
};
exports.useCollapsibleTableRows = useCollapsibleTableRows;
