import { TableSelectionType } from '../constants';
import { FastTableSelection } from '../contexts/TableSelectionContext';
import { TableSelection } from '../types';

/**
 * Return count of selected rows in {@link Table} `selection`.
 *
 * @param totalRows Total available rows count
 * @param selection Current {@link Table} selection
 */
export function selectedRowsCount(
  totalRows: number,
  selection?: TableSelection | FastTableSelection,
): number {
  let result: number;
  switch (selection?.type) {
    case TableSelectionType.Include:
      result = Array.isArray(selection.selectedIds)
        ? selection.selectedIds.length
        : selection.selectedIds.size;
      break;
    case TableSelectionType.Exclude:
      result =
        totalRows -
        (Array.isArray(selection.unselectedIds)
          ? selection.unselectedIds.length
          : selection.unselectedIds.size);
      break;
    default:
      return 0;
  }

  return result;
}
