import type { Block , Mobilization , State , Widget , StatePlugins } from "./reducers" ; export interface StatusFilter { status : 'active' | 'archived' ; } export interface ISelectors { getMobilization : ( ) => Mobilization | undefined ; mobilizationsIsLoading : ( ) => boolean ; mobilizationsIsLoaded : ( ) => boolean ; mobilizationIsSaving : ( ) => boolean ; getMobilizationMenuActive : ( ) => number | undefined ; getMobilizations : ( f ? : StatusFilter ) => Mobilization [ ] ; hasCurrentMobilization : ( ) => number | undefined ; hasMouseOver : ( key : string , id : number ) => boolean ; getBlocks : ( ) => Block [ ] ; getBlockLastPosition : ( ) => number ; blocksIsLoaded : ( ) => boolean ; canMoveUp : ( ) => boolean ; canMoveDown : ( ) => boolean ; getUploadProgress : ( key : string ) => number ; getEditing : ( ) => string | undefined ; getBlockSaving : ( ) => boolean ; getWidgets : ( ) => Widget [ ] ; getWidget : ( id : number ) => Widget | undefined ; mobilizationIsNeedReload : ( ) => boolean ; widgetsIsLoaded : ( ) => boolean ; widgetsIsLoading : ( ) => boolean ; renderIsLoading : ( ) => boolean ; getPlugin : ( plugin : keyof StatePlugins ) => any ; } export default ( state , properties ? ) : ISelectors => ( { getMobilization : ( ) : Mobilization | undefined => { const { currentId : id , data } = state . mobilizations ; return id ? data . find ( mob => mob . id === id ) : undefined ; } , mobilizationsIsLoading : ( ) : boolean => state . mobilizations . fetching , mobilizationsIsLoaded : ( ) : boolean => state . mobilizations . isLoaded , mobilizationIsSaving : ( ) : boolean => state . mobilizations . saving , getMobilizationMenuActive : ( ) : number | undefined => state . mobilizations . menuActiveIndex , getMobilizations : ( f ? ) : Mobilization [ ] => { const { data } = state . mobilizations ; if ( f ) return data . filter ( m => m . status === f . status ) ; return data . filter ( m => m . status === 'active' ) ; } , hasCurrentMobilization : ( ) : number | undefined => state . mobilizations . currentId , hasMouseOver : ( key , id ) : boolean => { const { hover } = state ; if ( key in hover ) return hover [ key ] === id ; return false ; } , getBlocks : ( ) : Block [ ] => { const { blocks : { data } } = state ; return data . sort ( ( a , b ) => a . position - b . position ) ; } , getBlockLastPosition : ( ) : number => { const { blocks : { data } } = state ; const lastIndex = data . length - 1 ; return data . length > 0 ? data . sort ( ( a , b ) => a . position - b . position ) [ lastIndex ] . position : 0 ; } , blocksIsLoaded : ( ) : boolean => state . blocks . isLoaded , canMoveUp : ( ) : boolean => { const { block } = properties ; const { blocks : { data } } = state ; return data [ 0 ] && data [ 0 ] . id !== block . id ; } , canMoveDown : ( ) : boolean => { const { block } = properties ; const { blocks : { data } } = state ; const index = data . length - 1 ; return data [ index ] && data [ index ] . id !== block . id ; } , getUploadProgress : ( key ) : number => state . uploader [ key ] , getEditing : ( ) : string | undefined => { const { edition } = state ; return edition . isEditing ? edition . mode : undefined ; } , getBlockSaving : ( ) : boolean => { const { blocks : { saving } } = state ; return saving ; } , getWidgets : ( ) : Widget [ ] => state . widgets . data , getWidget : ( id ) : Widget | undefined => { const { widgets : { data , currentId } } = state ; let pkey ; if ( properties ?. match . params ?. widget_id ) { pkey = Number . parseInt ( properties . match . params . widget_id , 10 ) ; } else if ( id ) { pkey = id ; } else { pkey = currentId || 0 ; } return data . find ( w => w . id === pkey ) ; } , mobilizationIsNeedReload : ( ) : boolean => { const { mobilizations : { reload } , blocks : { isLoaded : blocksIsLoaded } , widgets : { isLoaded : widgetsIsLoaded } } = state ; return ! ! ( ! blocksIsLoaded || ! widgetsIsLoaded ? true : reload ) ; } , widgetsIsLoaded : ( ) : boolean => state . widgets . isLoaded , widgetsIsLoading : ( ) : boolean => state . widgets . saving , renderIsLoading : ( ) : boolean => { const { widgets : { isLoaded : widgetsIsLoaded , fetching : widgetsIsLoading } , blocks : { isLoaded : blocksIsLoaded , fetching : blocksIsLoading } } = state ; const isLoaded = blocksIsLoaded && widgetsIsLoaded ; const isntLoading = ! blocksIsLoading && ! widgetsIsLoading ; return ! ( isLoaded && isntLoading ) ; } , getPlugin : ( plugin ) : any => state . plugins [ plugin ] } ) ;