import { createSelector } from 'reselect'
import { MettaSvr_Common } from "../../redux/helpers/action-enums"
import { menuItems } from './horizontal-sidebar/horizontal-sidebar.initial-values'

export const getCurrentUser = state => state.user.currentUser
export const getConnectedStatus  = state  => state.remoteNotification.isConnected
export const getRemoteNotificationErrorMessages = state =>  state.remoteNotification.errorMessages
export const getRemoteNotifications = state => state.remoteNotification.result
export const getIsSideBarOpen = state => state.user.sidebarOpen
export const getCurrentUseProfilePhotoUrl = state => state.user.currentUser.profilePhotoUrl
    

export const getCurrentUserMenu = createSelector(
    getCurrentUser,
    user => user.menu

)

export const getRemoteNotificationItemCount = createSelector(
    getRemoteNotifications,
    (notifications) => notifications.reduce((accumulatedCount, item) => accumulatedCount + item.count, 0),
) 

export const getCountOfWorkingNotificationStatus = createSelector(
    getRemoteNotifications,
    (notifications) => {
        if(notifications.length > 0){

            const type = parseInt(MettaSvr_Common.NotificationTypeEnum.Working)
            let pos = notifications.map((e) =>  (e.notificationType) ).indexOf(type)
            return !!notifications[pos] ? notifications[pos].count : 0
        }
    }
    
)
    
export const getCountOfHealthNotificationStatus = createSelector(
    getRemoteNotifications,
    (notifications) => {
        if(notifications.length > 0){

            const type = parseInt(MettaSvr_Common.NotificationTypeEnum.HealthStatus)
            let pos = notifications.map((e) =>  (e.notificationType) ).indexOf(type)
            return !!notifications[pos] ? notifications[pos].count : 0
        }
    }
    
)

export const getCountOfSystemNotificationStatus = createSelector(
    getRemoteNotifications,
    (notifications) => {
        if(notifications.length > 0){

            const type = parseInt(MettaSvr_Common.NotificationTypeEnum.System)
            let pos = notifications.map((e) =>  (e.notificationType) ).indexOf(type)
            return !!notifications[pos] ? notifications[pos].count : 0
        }
    }           
)

export const getShouldShowMenu = menu_identifier => createSelector(
    getCurrentUserMenu,
    (menuItems) => menuItems.filter( theMenu => theMenu.link_url === menu_identifier).length > 0

)