import LabReportActionTypes from './lab-report.action.types'

const INITIAL_STATE = {
    result: [],
    isFetching: false,
    errorMessage: undefined,
    submissionInProgress: false,
    saveResult: null,
    recordCount: 0,  
    labReportResult: undefined,
    labReportDownloadInProgress: false
}

const labReportReducer = (currentState = INITIAL_STATE, action) => {
    switch(action.type){
        case LabReportActionTypes.RESET_STATE:
            return INITIAL_STATE
            
        case LabReportActionTypes.CREATE_LAB_REPORT_START:{
            return{
                ...currentState,
                submissionInProgress: true
            }
        }
        case LabReportActionTypes.CREATE_LAB_REPORT_SUCCESS:{
            return{
                ...currentState,
                errorMessage: null,
                submissionInProgress: false,
                saveResult: action.payload,
            }
        }
        
        case LabReportActionTypes.FETCH_LAB_REPORT_START:{
            return{
                ...currentState,
                isFetching: true
            }
        }
        case LabReportActionTypes.FETCH_LAB_REPORT_SUCCESS:
            const { count, records } = action.payload

            return {
                ...currentState,
                isFetching: false,
                result: records,
                errorMessage: null,
                recordCount: count 
            }
        case LabReportActionTypes.FETCH_LAB_REPORT_FAILURE:
            const { message } = action.payload

            return{
                ...currentState,
                isFetching: false,
                errorMessage: message
            }
        case LabReportActionTypes.FETCH_LAB_REPORT_LIST_CLEAR_ERROR_STATE:
            return{
                ...currentState,
                errorMessage: null,
                isFetching: false,
                submissionInProgress: false,
                saveResult: null,
            }
        //Download lab report
        case LabReportActionTypes.DOWNLOAD_LAB_REPORT_START:{
            return {
                ...currentState,
                labReportDownloadInProgress: true,
                errorMessages: null
            }
        }
        case LabReportActionTypes.DOWNLOAD_LAB_REPORT_SUCCESS:{
            const { /*count,*/ records } = action.payload
            return {
                ...currentState,
                labReportDownloadInProgress: false,
                errorMessages: null,
                labReportResult: records
            }
        }
        case LabReportActionTypes.DOWNLOAD_LAB_REPORT_FAILURE:{
            return {
                ...currentState,
                labReportDownloadInProgress: false,
                labReportResult: undefined,
                errorMessages: action.payload.message
            }
        }            
        default:
            return currentState
    }
}


export default labReportReducer