import axios from 'axios'
// import { TokenService } from '@/services/token.service'
// import store from '@/store/'

const ApiService = {
  // Stores the 401 interceptor position so that it can be later ejected when needed
  _401interceptor: null,

  init (baseURL) {
    axios.defaults.baseURL = baseURL
  },

  setHeader () {
    axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'
    // eslint-disable-next-line dot-notation
    axios.defaults.headers.common['Authorization'] = `Bearer `
  },

  removeHeader () {
    axios.defaults.headers.common = {}
  },

  query (resource, params) {
    return axios.get(resource, params).catch(error => {
      console.log(error)
    })
  },

  get (resource, slug = '') {
    return axios.get(`${resource}/${slug}`)
  },

  post (resource, params) {
    return axios.post(`${resource}`, params)
  },

  update (resource, slug, params) {
    return axios.put(`${resource}/${slug}`, params)
  },

  put (resource, params) {
    return axios.put(`${resource}`, params)
  },

  delete (resource, params) {
    return axios.delete(resource, params)
  },

  /**
     * Perform a custom Axios request.
     *
     * data is an object containing the following properties:
     *  - method
     *  - url
     *  - data ... request payload
     *  - auth (optional)
     *    - username
     *    - password
    **/
  customRequest (data) {
    return axios(data)
  },
}

export default ApiService
