import { selectAll, selectionStatus } from '../../data-tools' import type { GridRowId } from '../../types' import type { GridState } from '../reducer' import type { StoreSimpleSelectors } from '../selectors' import type { SelectFilterMatches, SelectFilterMatchesShowOnly } from './filter' export default function generateSelectors( selectors: Pick< StoreSimpleSelectors, 'selectSelectionMode' | 'selectRowCollection' | 'selectSelection' >, selectFilterMatches: SelectFilterMatches, selectFilterMatchesShowOnly: SelectFilterMatchesShowOnly ) { const selectRowSelectionState = (state: GridState, rowId: GridRowId) => { const selectionMode = selectors.selectSelectionMode(state) if (selectionMode === 'none') { return 'none' } const rows = selectors.selectRowCollection(state) const matches = selectFilterMatchesShowOnly(state) const selection = selectors.selectSelection(state) return selectionStatus(rows, selection, rowId, matches) } const selectIsRowSelected = (state: GridState, rowId: GridRowId) => selectRowSelectionState(state, rowId) === 'all' const selectIsRowVisuallySelected = ( state: GridState, rowId: GridRowId ) => { const selection = selectors.selectSelection(state) return selection.has(rowId) } const selectSelectionStatus = (state: GridState) => { const rows = selectors.selectRowCollection(state) const matches = selectFilterMatchesShowOnly(state) const selection = selectors.selectSelection(state) return selectionStatus(rows, selection, null, matches) } const selectAllSelectableIds = (state: GridState) => { const rows = selectors.selectRowCollection(state) const matches = selectFilterMatchesShowOnly(state) return selectAll(rows, matches) } const selectRowExcluded = (state: GridState, rowId: GridRowId) => { const matchingIds = selectFilterMatches(state) if (!matchingIds.necessaryMatchedIds) { return false } return !matchingIds.necessaryMatchedIds.has(rowId) } return { selectRowSelectionState, selectIsRowSelected, selectIsRowVisuallySelected, selectSelectionStatus, selectAllSelectableIds, selectRowExcluded, } } export type SelectRowSelectionState = ReturnType< typeof generateSelectors >['selectRowSelectionState'] export type SelectIsRowSelected = ReturnType< typeof generateSelectors >['selectIsRowSelected'] export type SelectIsRowVisuallySelected = ReturnType< typeof generateSelectors >['selectIsRowVisuallySelected'] export type SelectSelectionStatus = ReturnType< typeof generateSelectors >['selectSelectionStatus'] export type SelectAllSelectableIds = ReturnType< typeof generateSelectors >['selectAllSelectableIds'] export type SelectRowExcluded = ReturnType< typeof generateSelectors >['selectRowExcluded']