import { takeLatest,  all, call, select } from 'redux-saga/effects'
import buildQuery from 'odata-query'
import DietPlanActionTypes from './diet-plan.action.types'
import { getCurrentUserId } from '../../user/selectors'
import {METADATA_END_POINTS} from '../../helpers/action-urls'
import {MettaSvr_Common} from '../../helpers/action-enums'
import { handleGeneralChannelCallbacks, handleODataChannelCallbacks } from '../../helpers/common-sagas'
import {fetchDietPlanListSuccess, fetchDietPlanListFailure, createDietPlanSuccess,
createDietPlanFailure
} from './diet-plan.action'




export function* fetchDietPlanListAsync({payload}){
      
      const { GET_DIET_CHART } = METADATA_END_POINTS.patient
      
      const config = {
        url: `${GET_DIET_CHART(payload)}`,
        method: 'get',
      }


      yield call(handleGeneralChannelCallbacks, config, fetchDietPlanListSuccess, fetchDietPlanListFailure)
  
}

export function* createDietPlanStartAsync({payload}){
      const { REGISTER_DIET_CHART } = METADATA_END_POINTS.patient
      const config = {
        url: REGISTER_DIET_CHART,
        method: 'post',
        data: payload
      }

      yield call(handleGeneralChannelCallbacks, config, createDietPlanSuccess, createDietPlanFailure)
  
}

//listeners
export function* onFetchDietPlanListStart(){
  yield takeLatest(DietPlanActionTypes.FETCH_DIET_PLAN_START, fetchDietPlanListAsync)
}

export function* onDietPlanCreateStart(){
  yield takeLatest(DietPlanActionTypes.CREATE_DIET_PLAN_START, createDietPlanStartAsync)
}


//final visitor sagas shared
export function* dietPlanSagas() {
    yield all([
      call(onFetchDietPlanListStart),      
      call(onDietPlanCreateStart),
      ])
  }