import { takeLatest, all, call, select } from 'redux-saga/effects'
import buildQuery from 'odata-query'

import {METADATA_END_POINTS} from '../helpers/action-urls'
import VisitorActionTypes from './visitor.action.types'
import { fetchVisitorListSuccess, updateVisitorSuccess, visitorListFailure} from './visitor.action'
import { getCurrentUserId} from './selectors'
import { handleGeneralChannelCallbacks, handleODataChannelCallbacks } from '../helpers/common-sagas'

/*
dispatch({
                type: UserActionTypes.UPDATE_ACCESS_TOKEN,
                payload: refresh_token
            })
*/

export function* updateVisitorAsync({payload}){
    
    const { UPDATE_VISITOR } = METADATA_END_POINTS.admin
    const config = {
        url: `${UPDATE_VISITOR}`,
        method: 'post',
        data: payload
      }

    const successCb = () => updateVisitorSuccess(payload.VisitorId)
    yield call(handleGeneralChannelCallbacks, config, successCb, visitorListFailure)
}
 
export function* fetchVisitorListAsync({payload}){
        console.log(' before fetching ', payload)
        const currentUserId = yield select(getCurrentUserId) //only when patient logs in
        const { GET_VISITOR_LIST } = METADATA_END_POINTS.common
        const odataQuery = {
            filter:{
                PatientId: { eq: { type: 'guid', value: payload ?? currentUserId } },
                IsActive: true            
            },
        }
        
        const query = buildQuery( odataQuery )
        
        const config = {
            url: `${GET_VISITOR_LIST}${query}`
          }
        
        yield call(handleODataChannelCallbacks, config, fetchVisitorListSuccess, visitorListFailure)
      
}

export function* deactivateVisitorAsync({payload}){
    
    const { UPDATE_VISITOR_STATUS } = METADATA_END_POINTS.admin
    const config = {
        url: `${UPDATE_VISITOR_STATUS}`,
        method: 'post',
        data: payload
      }

    const successCb = () => updateVisitorSuccess(payload.UserId)
    yield call(handleGeneralChannelCallbacks, config, successCb, visitorListFailure)
}

export function* createVisitorStartAsync({payload}){
    
    const { REGISTER_VISITOR, API_VERSION_V1 } = METADATA_END_POINTS.auth
    const config = {
        url: `${API_VERSION_V1}${REGISTER_VISITOR}`,
        method: 'post',
        data: payload
      }

    yield call(handleGeneralChannelCallbacks, config, updateVisitorSuccess, visitorListFailure)
  
}
//listeners
export function* onFetchVisitorStart(){
    yield takeLatest(VisitorActionTypes.FETCH_VISITOR_START, fetchVisitorListAsync)
}

export function* onVisitorUpdateStart(){
    yield takeLatest(VisitorActionTypes.UPDATE_VISITOR_START, updateVisitorAsync)
}

export function* onVisitorDeactivateStart(){
    yield takeLatest(VisitorActionTypes.DEACTIVATE_VISITOR_START, deactivateVisitorAsync)
}

export function* onVisitorCreateStart(){
    yield takeLatest(VisitorActionTypes.CREATE_VISITOR_START, createVisitorStartAsync)
}

//final visitor sagas shared
export function* visitorSagas() {
    yield all([
      call(onFetchVisitorStart),   
      call(onVisitorUpdateStart),
      call(onVisitorDeactivateStart),
      call(onVisitorCreateStart)   
    ])
  }