import { createSelector } from 'reselect'
import { MettaSvr_Common } from "../helpers/action-enums"

export const getCurrentUser = state => state.user.currentUser
export const getVisitor = state => state.visitors
export const getTargetVisitorId = state => state.visitors.targetVisitorId
export const getVisitorList = createSelector(
    getVisitor,
     visitors => visitors.result
    )
export const getVisitorIsFetching = state => state.visitors.isFetching
export const getErrorMessage = state => state.visitors.errorMessage
export const getIsPatient = createSelector(
    getCurrentUser,
    user => user.userType === MettaSvr_Common.UserTypeEnum.Patients
)
export const getAccessToken = createSelector(
    getCurrentUser,
    user => user.accessToken
)
export const getCurrentUserId = createSelector(
    getCurrentUser,
    user => user.userId
)

export const getIsFetching = createSelector(
    getVisitor,
    visitors => visitors.isFetching || visitors.submissionInProgress
)

export const getSavedResult = createSelector(
    getVisitor,
    visitors => visitors.saveResult
)
export const getCurrentVisitor = createSelector(
    [getTargetVisitorId, getVisitorList],
    (theVisitorId, visitors) => {
        const output = visitors.filter((visitor) => visitor.VisitorId === theVisitorId)
        return output.length === 0 ? null : output[0]
    }
)