import webapi from './webapi'
import {fetch} from '@ttk/utils'
import { clearUserInfo, goToLogin, closeTabAction, openTabAction } from '../app-root/action'
// import { commit } from '@ttk/app-loader'

export function closeTab(reduce, gf, data){
  return async(dispatch, getState)=>{
    dispatch(closeTabAction(null, null, data))
  }
}
export function openTab(reduce, gf, data){
  return async(dispatch, getState)=>{
    dispatch(openTabAction(null, null, data))
  }
}

export function setCurrentTab(reduce, gf, data) {
  return async (dispatch, getState) => {
    reduce('tabs', { type: 'setCurrentTab', data })
  }
}

// 注意：action可以复用，但如果被调用的action函数里有使用reduce或gf函数，这时reduce或fg函数是无法正常调用的。因为reduce和gf的作用域都只在当前app
// 下面就是一个复用action的实例
export function logoutAction(reduce, gf, data) {
  return async (dispatch, getState) => {
    // dispatch(commit('app-root/dateRange', {type:'e33311ss', data:{res:'ceshi '}}))
    const token = fetch.getAccessToken()
    const res = await webapi.logout({ token, interceptConfigAll: true })
    dispatch(clearUserInfo())
    dispatch(goToLogin())
    return res
  }
}