import request from './request';
import store from "@/store"

export function getAccessToken(){
  return store.getters.accessToken
}
export function setAccessToken(accessToken){
  store.commit('SET_ACCESS_TOKEN',accessToken)
}

export function setRefreshToken (refreshToken){
  store.commit('SET_REFRESH_TOKEN',refreshToken)
}

let promise;
export async function refreshToken(){
  if(promise){
    return promise;
  }
  promise = new Promise(async (resolve)=>{
    const resp = await request({
      url: '/maintain/token/refresh',
      method: 'post',
      data: {
        scope: store.getters.scope,
        token: store.getters.refreshToken,
        id: 'chelvc_client',
        secret: 'chelvc@secret#2023!'
      },
      _isRefreshToken: true
    })
    resolve(resp.code === 'OK')
  })
  promise.finally(()=>{
    promise = null
  })
  return promise
}

export function isRefreshRequest(config){
  return !!config._isRefreshToken
}

