import type { GridRowId } from '../types' import type { Collection } from './types' export function selectionStatus( collection: Collection, selection: Set, rowId: GridRowId | null, matchedIds?: Set | null ): 'all' | 'some' | 'none' { if (rowId !== null) { const meta = collection.meta.get(rowId) if (meta?.type !== 'group') { return selection.has(rowId) ? 'all' : 'none' } } const collect = (nodeId: GridRowId, memo: GridRowId[]) => { const meta = collection.meta.get(nodeId) if (meta?.type !== 'group') { if (!matchedIds || matchedIds.has(nodeId)) { memo.push(nodeId) } } if (meta?.children?.length) { meta.children.forEach((childId) => collect(childId, memo)) } } const ids = rowId !== null ? [rowId] : collection.ids const memo: string[] = [] ids.forEach((id) => collect(id, memo)) if (memo.length === 0) { return 'none' } if (memo.every((id) => selection.has(id))) { return 'all' } if (memo.some((id) => selection.has(id))) { return 'some' } return 'none' }