import {PatientListActionTypes } from './patient-list.action.type'

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

const patientListReducer = (currentState = INITIAL_STATE, action) => {
    switch(action.type){
        case PatientListActionTypes.GET_PATIENT_LIST_START:
            return{
                ...currentState,
                inProgress: true,

            }
        case PatientListActionTypes.SET_TARGET_PATIENT:
            const { identifier = null, index = -1} = action.payload
            return{
                ...currentState,
                targetPatientId: identifier,
                updateIndex: index,
                saveResult: null
            }
        case PatientListActionTypes.GET_PATIENT_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 PatientListActionTypes.GET_PATIENT_LIST_FAILURE:{
            const { message } = action.payload
            return{
                ...currentState,
                errorMessages: message,
                result:[],
                inProgress: false
            }
        }
        
        case PatientListActionTypes.UPDATE_PATIENT_START:{
            const {indexOfRecord } = action.payload
            return {
                ...currentState,
                submissionInProgress: true,
                updateIndex: indexOfRecord,
                errorMessages: null
            }
        }
        case PatientListActionTypes.UPDATE_PATIENT_SUCCESS:{
            const updatedResult = [...currentState.result]            
            // const {IsActive } = updatedResult[currentState.updateIndex]
            const{ fieldName, updatedValue } = action.payload
            updatedResult[currentState.updateIndex][fieldName] = updatedValue 

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

        case PatientListActionTypes.UPDATE_PATIENT_FAILURE:{
            const {message} = action.payload
            return {
                ...currentState,
                submissionInProgress: false,
                errorMessages: message,        
            }
        }


        case PatientListActionTypes.PATIENT_LIST_CLEAR_ERROR_STATE:
            return{
                ...currentState,
                errorMessages: null,
                submissionInProgress: false,
                inProgress: false,
                saveResult: null
            }
        case PatientListActionTypes.PATIENT_LIST_RESET_STATE:
            return INITIAL_STATE

        default: {
            return currentState
        }
    }
}

export default patientListReducer