import DietPlanActionTypes from './diet-plan.action.types'

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

const dietPlanReducer = (currentState = INITIAL_STATE, action) => {
    switch(action.type){
        case DietPlanActionTypes.RESET_STATE:
            return INITIAL_STATE
            
        case DietPlanActionTypes.CREATE_DIET_PLAN_START:{
            return{
                ...currentState,
                submissionInProgress: true
            }
        }
        case DietPlanActionTypes.CREATE_DIET_PLAN_SUCCESS:{
            return{
                ...currentState,
                errorMessage: null,
                submissionInProgress: false,
                saveResult: action.payload,
            }
        }
        case DietPlanActionTypes.CREATE_DIET_PLAN_FAILURE:{
            const { message } = action.payload
            return{
                ...currentState,
                errorMessage: message,
                submissionInProgress: false,                
            }
        }
        
        case DietPlanActionTypes.FETCH_DIET_PLAN_START:{
            return{
                ...currentState,
                isFetching: true
            }
        }
//Sample response:
/*
------
{
    "result": [
        {
            "dietPlanId": "6856f7f2-0c7e-4232-bd62-b2b88c5e8c6d",
            "patientId": "374cdd0d-db39-4a51-9045-2f574a24f4be",
            "dietPlan": "{\"phoneno\":\"123456\",\"patient\":{\"earlyMorning\":\"- item 01\\\\n- item 02\\\\n- item 03\",\"breakfast\":\"- item 1\\\\n- item 2\\\\n- item 3\",\"midMorning\":\"- item 10\\\\n- item 20\\\\n- item 30\",\"lunch\":\"- item 111\\\\n- item 222\\\\n- item 333\",\"evening\":\"- item 11\\\\n- item 22\\\\n- item 33\",\"dinner\":\"- item 1\\\\n- item 2\\\\n- item 3\\\\n- item 4\\\\n- item 5\\\\n- item 6\",\"guidelines\":\"- guid 1\\\\n- guid 2\\\\n- guid 3\",\"name\":\"abcd\",\"age\":\"24 yrs\",\"height\":\"23 cm\",\"weight\":\"40 kg\",\"bmi\":\"129\",\"ibw\":\"120\",\"condition\":\"- ok\\\\n- not ok\",\"calories\":\"good \\\\nbad\\\\nugly\"}}",
            "isActive": true,
            "createdOn": "21-03-2022",
            "modifiedOn": "",
            "createdBy": "e2541f6c-b12d-4fe1-943e-89517797cbdb",
            "modifiedBy": null
        }
    ],
    "apiResponseStatus": 1,
    "errorMessage": null,
    "refreshedAccessToken": null
}
------
*/
        case DietPlanActionTypes.FETCH_DIET_PLAN_SUCCESS:
            const records = action.payload
            let out = records.length > 0 ? records[records.length-1] : null
            return {
                ...currentState,
                isFetching: false,
                result: out,
                errorMessage: null,
                
            }
        case DietPlanActionTypes.FETCH_DIET_PLAN_FAILURE:
            const { message } = action.payload

            return{
                ...currentState,
                isFetching: false,
                errorMessage: message
            }
        case DietPlanActionTypes.FETCH_DIET_PLAN_LIST_CLEAR_ERROR_STATE:
            return{
                ...currentState,
                errorMessage: null,
                isFetching: false,
                submissionInProgress: false,
                saveResult: null,
            }
        default:
            return currentState
    }
}


export default dietPlanReducer