import {EducatorListActionTypes } from './educator-list.action.type'

const INITIAL_STATE = 
{
    inProgress: false,
    result: [],
    apiResponseStatus: -1,
    errorMessages: null,
    recordCount: 0,
    submissionInProgress: false,
    updateIndex: -1,
}

const educatorListReducer = (currentState = INITIAL_STATE, action) => {
    switch(action.type){
        case EducatorListActionTypes.GET_EDUCATOR_LIST_START:
            return{
                ...currentState,
                inProgress: true,

            }
        case EducatorListActionTypes.GET_EDUCATOR_LIST_SUCCESS:
            {
                /*
                *as for odata api this returns empty string for patients instead of array
                *hence safe type check
                */
            const { count, records } = action.payload
            // const downloaded = Array.isArray(action.payload) ? action.payload : []
            return{
                ...currentState,
                result: records,
                errorMessages: null,
                inProgress: false,
                recordCount: count
            }
        }
        case EducatorListActionTypes.GET_EDUCATOR_LIST_FAILURE:{
            const { message } = action.payload
            return{
                ...currentState,
                errorMessages: message,
                result:[],
                inProgress: false
            }
        }
        
        case EducatorListActionTypes.EDUCATOR_LIST_RESET_STATE:
            return INITIAL_STATE
//Post request handling starts

        case EducatorListActionTypes.EDUCATOR_STATUS_POST_MESSAGE_START:{
            const {indexOfRecord } = action.payload
            return {
                ...currentState,
                submissionInProgress: true,
                updateIndex: indexOfRecord,
                errorMessages: null
            }
        }
        case EducatorListActionTypes.EDUCATOR_STATUS_POST_MESSAGE_SUCCESS:{
            const updatedResult = [...currentState.result]            
            const {IsActive } = updatedResult[currentState.updateIndex]
            updatedResult[currentState.updateIndex].IsActive = !IsActive

            return {
                ...currentState,
                submissionInProgress: false,
                updateIndex: -1,
                result: updatedResult,
                errorMessages: null
            }
        }

        case EducatorListActionTypes.EDUCATOR_STATUS_POST_MESSAGE_FAILURE:{
            const {message} = action.payload
            return {
                ...currentState,
                submissionInProgress: false,
                errorMessages: message,
                updateIndex: -1,
        
            }
        }
        case EducatorListActionTypes.EDUCATOR_STATUS_CLEAR_ERROR_STATE: {
            return {
                ...currentState,
                submissionInProgress: false,
                updateIndex: -1,
                errorMessages: null,
                inProgress: false
            }
        }

        default: {
            return currentState
        }
    }
}

export default educatorListReducer