var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import React, { useMemo, forwardRef, useCallback } from 'react';
import { prop } from 'ramda';
import Table from 'react-components/dist/Table/Table';
import { defaultGetRowCellHeight, defaultRenderRowCell } from '../helpers/basicTable';
import { getTableHeadData } from './helpers';
import { BasicTableContext } from '../contexts/BasicTableContext';
import { RowCollapseContext } from '../contexts/RowCollapseContext';
import { useCollapsibleTableRows } from '../hooks/useCollapsibleTableRows';
import { COLUMN_FILTER_HEIGHT } from '../constants/common';
export var BasicTable = forwardRef(function (_a, ref) {
    var columnsData = _a.columnsData, rowsData = _a.rowsData, context = _a.context, sorting = _a.sorting, onSort = _a.onSort, _b = _a.renderRowCell, renderRowCell = _b === void 0 ? defaultRenderRowCell : _b, _c = _a.getRowCellHeight, getRowCellHeight = _c === void 0 ? defaultGetRowCellHeight : _c, filters = _a.filters, onFilter = _a.onFilter, headRowHeight = _a.headRowHeight, _d = _a.maxRowValuesCount, maxRowValuesCount = _d === void 0 ? Infinity : _d, autosizing = _a.autosizing, maxHeight = _a.maxHeight, getIdFromRowValue = _a.getIdFromRowValue, applyMaxValuesCountConstraint = _a.applyMaxValuesCountConstraint, otherProps = __rest(_a, ["columnsData", "rowsData", "context", "sorting", "onSort", "renderRowCell", "getRowCellHeight", "filters", "onFilter", "headRowHeight", "maxRowValuesCount", "autosizing", "maxHeight", "getIdFromRowValue", "applyMaxValuesCountConstraint"]);
    var tableHeadData = useMemo(function () { return getTableHeadData(columnsData, filters, onFilter); }, [columnsData, filters, onFilter]);
    var _e = useCollapsibleTableRows({
        rowsData: rowsData,
        columnsData: columnsData,
        renderRowCell: renderRowCell,
        getRowCellHeight: getRowCellHeight,
        maxRowValuesCount: maxRowValuesCount,
        getIdFromRowValue: getIdFromRowValue,
        applyMaxValuesCountConstraint: applyMaxValuesCountConstraint
    }), tableRowsData = _e.tableRowsData, collapseContextValue = _e.collapseContextValue;
    var toggleRowCollapse = collapseContextValue.toggleRowCollapse, getIsRowCollapsed = collapseContextValue.getIsRowCollapsed;
    var onStartDragRow = useCallback(function () {
        setTimeout(function () {
            tableRowsData.forEach(function (_, index) {
                if (!getIsRowCollapsed(index)) {
                    toggleRowCollapse(index);
                }
            });
        }, 0);
    }, [getIsRowCollapsed, toggleRowCollapse, tableRowsData]);
    if (!tableHeadData.length) {
        return null;
    }
    return (React.createElement(RowCollapseContext.Provider, { value: collapseContextValue },
        React.createElement(BasicTableContext.Provider, { value: context },
            React.createElement(Table, __assign({ autosizing: autosizing, maxHeight: maxHeight, defaultRowHeight: 48, onStartDragRow: onStartDragRow }, otherProps, { fixHead: tableRowsData.length > 0, headData: tableHeadData, rowsData: tableRowsData, sortField: prop('field', sorting), sortOrder: prop('order', sorting), onSort: onSort, headRowHeight: filters ? headRowHeight + COLUMN_FILTER_HEIGHT : headRowHeight, shouldRenderDndProvider: false, ref: ref })))));
});
BasicTable.displayName = 'BasicTable';
