import { TableRowSelectionState } from '../../types';
/**
 * Abstract base class for handling behavior associated with interactive row selection of the table.
 */
export class SelectionManagerBase {
    constructor(tanStackTable, actionMenusPreserveSelection) {
        this.tanStackTable = tanStackTable;
        this.actionMenusPreserveSelection = actionMenusPreserveSelection;
    }
    updateActionMenusPreserveSelection(actionMenusPreserveSelection) {
        this.actionMenusPreserveSelection = actionMenusPreserveSelection;
    }
    reset() { }
    toggleIsRowSelected(rowState, isSelecting) {
        if (rowState.isGroupRow
            && rowState.selectionState === TableRowSelectionState.selected) {
            // Work around for https://github.com/TanStack/table/issues/4759
            // Manually deselect all leaf rows when a fully selected group is being deselected.
            this.deselectAllLeafRows(rowState.id);
        }
        else {
            this.tanStackTable.getRow(rowState.id).toggleSelected(isSelecting, {
                selectChildren: rowState.isGroupRow
            });
        }
    }
    selectSingleRow(rowState) {
        if (rowState.isGroupRow) {
            throw new Error('function not intended to select grouped rows');
        }
        const currentSelection = this.tanStackTable.getState().rowSelection;
        const selectedRecordIds = [];
        Object.entries(currentSelection).forEach(([recordId, isSelected]) => {
            if (isSelected) {
                selectedRecordIds.push(recordId);
            }
        });
        if (selectedRecordIds.length === 1
            && selectedRecordIds[0] === rowState.id) {
            // The clicked row is already the only selected row. Do nothing.
            return false;
        }
        const newSelectionState = {};
        newSelectionState[rowState.id] = true;
        this.tanStackTable.setRowSelection(newSelectionState);
        return true;
    }
    deselectAllLeafRows(rowId) {
        const groupRow = this.tanStackTable.getRow(rowId);
        const leafRowIds = this.getAllLeafRowIds(groupRow.id);
        const selectionState = this.tanStackTable.getState().rowSelection;
        for (const id of leafRowIds) {
            delete selectionState[id];
        }
        this.tanStackTable.setRowSelection(selectionState);
    }
    getAllLeafRowIds(id) {
        const row = this.tanStackTable
            .getRowModel()
            .flatRows.find(x => x.id === id);
        if (!row?.getIsGrouped()) {
            return [];
        }
        return row
            .getLeafRows()
            .filter(leafRow => !leafRow.getIsGrouped())
            .map(leafRow => leafRow.id);
    }
    getAllOrderedRows() {
        const topLevelRows = this.tanStackTable.getPreExpandedRowModel().rows;
        return this.getOrderedRows(topLevelRows);
    }
    getOrderedRows(topLevelRows) {
        const allRows = [];
        for (const row of topLevelRows) {
            allRows.push(row);
            if (row.subRows?.length > 0) {
                allRows.push(...this.getOrderedRows(row.subRows));
            }
        }
        return allRows;
    }
}
//# sourceMappingURL=selection-manager-base.js.map