import { createSelector } from 'reselect' ; import { watcherFoldersSelector } from './index' ; import { Watcher , WatcherFolder , WatcherFolderMap , FolderId } from '../types' ; import { normalizedWatchers } from './watchers' ; import { Map , Set } from 'immutable' ; import { sortWatcherFoldersNewestFirst , sortWatchersNewestFirst } from '../utils/sorting' ; export const watcherFoldersSortedByCreationDate = createSelector ( [ watcherFoldersSelector ] , folders => folders . sort ( sortWatcherFoldersNewestFirst ) as WatcherFolderMap ) ; export const watchersToFolderWatcherMap = createSelector ( [ normalizedWatchers , watcherFoldersSelector ] , ( watchers , folders ) => watchers . reduce ( ( acc , cur ) => acc . update ( cur . folderId , ( watcherArray ) => watcherArray && watcherArray . length > 0 ? [ ... watcherArray , cur ] : [ cur ] ) , Map < FolderId , Watcher [ ] > ( ) ) ) ; export const sortedFolderWatcherMap = createSelector ( [ watchersToFolderWatcherMap ] , watcherFolderMap => watcherFolderMap . map ( ( watchers ) => watchers . sort ( sortWatchersNewestFirst ) ) as Map < FolderId , Watcher [ ] > ) ; export const getWatchersAssignedToFolder = ( folderId ) => createSelector ( [ watchersToFolderWatcherMap ] , watcherFolderMap => watcherFolderMap . get ( folderId , [ ] ) ) ; export const getWatcherIdsAssignedToFolder = ( folderId ) => createSelector ( [ getWatchersAssignedToFolder ( folderId ) ] , watchers => watchers . map ( ( cur ) => cur . groupId ) ) ; export const getNumWatchersAssignedToFolder = ( folderId ) => createSelector ( [ getWatchersAssignedToFolder ( folderId ) ] , watchers => watchers . length ) ; export const watcherFolderUniqueNames = createSelector ( [ watcherFoldersSelector ] , watcherFolders => watcherFolders . reduce ( ( acc , cur ) => acc . add ( cur . name ) , Set < string > ( ) ) ) ;