import { __assign } from "tslib";
function setRowSpan(column, rows, currentRow) {
    var rowSpan = rows.length - currentRow;
    if (column &&
        !column.children &&
        rowSpan > 1 &&
        (!column.rowSpan || column.rowSpan < rowSpan)) {
        column.rowSpan = rowSpan;
    }
}
export function groupedColumns(columns, currentRow, parentColumn, rows) {
    if (currentRow === void 0) { currentRow = 0; }
    if (parentColumn === void 0) { parentColumn = {}; }
    if (rows === void 0) { rows = []; }
    rows[currentRow] = rows[currentRow] || [];
    var grouped = [];
    columns.forEach(function (column, index) {
        var newColumn = __assign({}, column);
        rows[currentRow].push(newColumn);
        parentColumn.colSpan = parentColumn.colSpan || 0;
        if (newColumn.children && newColumn.children.length > 0) {
            if (newColumn.needSort) {
                newColumn.needSort = false;
            }
            newColumn.children = groupedColumns(newColumn.children, currentRow + 1, newColumn, rows);
            parentColumn.colSpan += newColumn.colSpan;
        }
        else {
            parentColumn.colSpan++;
        }
        for (var i = 0; i < rows[currentRow].length - 1; ++i) {
            setRowSpan(rows[currentRow][i], rows, currentRow);
        }
        if (index + 1 === columns.length) {
            setRowSpan(newColumn, rows, currentRow);
        }
        grouped.push(newColumn);
    });
    return grouped;
}
export function getLeafColumns(columns) {
    var leafColumns = [];
    columns.forEach(function (column) {
        if (!column.children) {
            leafColumns.push(column);
        }
        else {
            leafColumns.push.apply(leafColumns, getLeafColumns(column.children));
        }
    });
    return leafColumns;
}
export function needFixBatchComps(isTableInView, isHeaderInView, isFootInView) {
    if (isTableInView && !isHeaderInView && !isFootInView) {
        return true;
    }
    return false;
}
export function isElementInView(el, offset) {
    if (offset === void 0) { offset = 0; }
    if (el) {
        var footerRect = el.getBoundingClientRect();
        var footerY = footerRect.top - document.documentElement.getBoundingClientRect().top;
        return (footerY + footerRect.height - offset > window.pageYOffset &&
            footerY <= window.pageYOffset + window.innerHeight - offset);
    }
    else {
        return false;
    }
}
export function mapDOMNodes(nodes, callback) {
    var result = [];
    if (!nodes) {
        return result;
    }
    for (var i = 0; i < nodes.length; i++) {
        result.push(callback(nodes[i], i));
    }
    return result;
}
export function getCompatSelectionPropsFn(selection) {
    return (selection === null || selection === void 0 ? void 0 : selection.getSelectionProps) || (selection === null || selection === void 0 ? void 0 : selection.getCheckboxProps);
}
export function getSelectAllCheckboxState(datasets, getRowIndex, getRowSelectionState) {
    return (datasets || []).reduce(function (state, row, index) {
        var rowIndex = getRowIndex(row, index);
        var itemSelectionState = getRowSelectionState(row, rowIndex);
        if (itemSelectionState === null || itemSelectionState === void 0 ? void 0 : itemSelectionState.disabled) {
            state.disabledRows.push(row);
        }
        else {
            state.enabledRows.push(row);
        }
        state.allDisabled = state.allDisabled && !!itemSelectionState.disabled;
        return state;
    }, {
        enabledRows: [],
        disabledRows: [],
        allDisabled: true,
    });
}
