import {MedicalLogTypes } from './medical-logs.action.types'

const INITIAL_STATE = 
{
    inProgress: false,
    result: null,
    orderDetails: null,
    apiResponseStatus: -1,
    errorMessages: null,
    currentPage: 0,
    medicalLogReportDownloadInProgress: false,
    medicalLogReportResult: null,

}

const medicalLogReducer = (currentState = INITIAL_STATE, action) => {
    switch(action.type){
        case MedicalLogTypes.GET_USER_MEDICAL_HISTORY_LOGS_START:
            return{
                ...currentState,
                inProgress: true

            }
        case MedicalLogTypes.GET_USER_MEDICAL_HISTORY_LOGS_SUCCESS:
            var targetObject = null
            
            if(!!action.payload){
                const { smokeLog, alcoholLog, depressionLog, erectileLog, ...rest} = action.payload
                targetObject = {                    
                    medicalLog: {...rest},
                    smokeLog: JSON.parse(smokeLog), 
                    alcoholLog: JSON.parse(alcoholLog), 
                    depressionLog: JSON.parse(depressionLog), 
                    erectileLog: JSON.parse(erectileLog),
                    
                }
            }

            return{
                ...currentState,
                result: targetObject,
                errorMessages: null,
                inProgress: false,

            }

        case MedicalLogTypes.GET_USER_MEDICAL_HISTORY_LOGS_FAILURE:
            const { message, apiResponseStatus} = action.payload
            return{
                ...currentState,
                errorMessages: message,
                apiResponseStatus: apiResponseStatus,
                inProgress: false
            }
        
        case MedicalLogTypes.GET_USER_MEDICAL_HISTORY_LOGS_REPORT_START:
            {
                return {
                    ...currentState,
                    errorMessages: null,
                    medicalLogReportDownloadInProgress: true
                }
            }
        case MedicalLogTypes.GET_USER_MEDICAL_HISTORY_LOGS_REPORT_SUCCESS:
            {
                const { /*count,*/ records } = action.payload

                return {
                    ...currentState,
                    medicalLogReportDownloadInProgress: false,
                    errorMessages: null,
                    medicalLogReportResult: records
                }
            }
       
        case MedicalLogTypes.GET_USER_MEDICAL_HISTORY_LOGS_REPORT_FAILURE:
            {
                const { /*count,*/ message } = action.payload
                return {
                    ...currentState,
                    medicalLogReportDownloadInProgress: false,
                    errorMessages: message,
                    medicalLogReportResult: undefined
                }
            }
        case MedicalLogTypes.MEDICAL_LOG_RESET_STATE:
            return INITIAL_STATE

        default: {
            return currentState
        }
    }
}

export default medicalLogReducer