import axios from '@/libs/api.request'

export const getNowActive = (parmas) => {
  return new Promise((resolve, reject) => {
    return axios
      .request({
        url: 'api/v1/rbac/cache/session/count',
        params: parmas,
        method: 'get',
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

export const getOldRbacCacheVer = (parmas) => {
  return new Promise((resolve, reject) => {
    return axios
      .request({
        url: 'api/v1/rbac/cache/permissions/cacheVersion',
        params: parmas,
        method: 'get',
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

export const getNowRbacVer = (parmas) => {
  return new Promise((resolve, reject) => {
    return axios
      .request({
        url: 'api/v1/rbac/cache/permissions/version',
        params: parmas,
        method: 'get',
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

export const refreshRbacVer = () => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: 'api/v1/rbac/cache/permissions/refresh',
        method: 'get',
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

export const getActiveUserData = (userId) => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: 'api/v1/rbac/cache/session/get/' + userId,
        method: 'get',
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

export const destroyActiveUser = (token) => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: 'oauth/revokeToken',
        method: 'post',
        data: { token: token },
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}
