import VisitorActionTypes from './visitor.action.types'

const INITIAL_STATE = {
    result: [],
    isFetching: false,
    errorMessage: undefined,
    saveResult: null,
    submissionInProgress: false,
    targetVisitorId: null
}

const visitorReducer = (currentState = INITIAL_STATE, action) => {
    switch(action.type){
        case VisitorActionTypes.RESET_STATE:
            return INITIAL_STATE
            
        case VisitorActionTypes.UPDATE_VISITOR_START:
            return{
                ...currentState,
                submissionInProgress: true                
            }

        case VisitorActionTypes.CREATE_VISITOR_START:{
            return{
                ...currentState,
                submissionInProgress: true
            }
        }
        case VisitorActionTypes.SET_CURRENT_VISITOR:
            return{
                ...currentState,
                targetVisitorId: action.payload
            }
        case VisitorActionTypes.UPDATE_VISITOR_SUCCESS:
            return{
                ...currentState,
                submissionInProgress: false,         
                errorMessage: null,
                saveResult: action.payload
            }
        case VisitorActionTypes.FETCH_VISITOR_SUCCESS:
            const { records } = action.payload

            return {
                ...currentState,
                isFetching: false,
                result: records,
                errorMessage: null
            }
        case VisitorActionTypes.VISITOR_FAILURE:
            const { message } = action.payload

            return{
                ...currentState,
                isFetching: false,
                errorMessage: message
            }
        case VisitorActionTypes.VISITOR_LIST_CLEAR_ERROR_STATE:
            return{
                ...currentState,
                errorMessage: null,
                isFetching: false,
                submissionInProgress: false,
                saveResult: null,
                targetVisitorId: action.payload
            }
        default:
            return currentState
    }
}


export default visitorReducer