import { createSelector } from 'reselect' ; import { Watcher , WatcherMap , RootState , WatcherFolderMap , GroupId } from '../types' ; import { watchersSelector , watcherFoldersSelector } from './index' ; import { Map , Set } from 'immutable' ; import { DEFAULT_WATCHER_FOLDER_ID } from '../constants/misc' ; import { watcherDefaults } from '../utils/watchers' ; /**
 * For backwards compatibility. Legacy watchers won't have certain properties.
 * Here we ensure they do.
 */ const updateLegacyWatchers = createSelector ( [ watchersSelector , watcherFoldersSelector ] , ( watchers , folders ) : WatcherMap => watchers . reduce ( ( acc , cur ) => acc . set ( cur . groupId , { ... watcherDefaults , ... cur , folderId : folders . has ( cur . folderId ) ? cur . folderId : DEFAULT_WATCHER_FOLDER_ID } ) , Map < GroupId , Watcher > ( ) ) ) ; export const normalizedWatchers = createSelector ( [ watchersSelector , updateLegacyWatchers ] , ( allWatchers , legacyWatchers ) : WatcherMap => allWatchers . merge ( legacyWatchers ) ) ; export const watcherIdsSet = createSelector ( [ normalizedWatchers ] , ( watchers ) => watchers . reduce ( ( acc , watcher ) => acc . add ( watcher . groupId ) , Set ( [ ] ) ) ) ; export const watcherTitlesMap = createSelector ( [ normalizedWatchers ] , ( watchers ) => watchers . reduce ( ( acc , watcher ) => acc . set ( watcher . groupId , watcher . title ) , Map < GroupId , string > ( ) ) ) ; export const getWatcher = ( id ) => ( state ) => normalizedWatchers ( state ) . get ( id ) ;