import axios from 'axios';

let links

export default async function AJAX({
  url,
  resource,
  id,
  method = 'GET',
  data = {},
  params = {},
  headers = {},
}) {
  try {
    if (window.basepath) {
      url = `${window.basepath}${url}`
    }

    if (resource) {
      if (!links) {
        const linksRes = await axios({
          url: '/chronograf/v1/',
          method: 'GET',
        })
        links = linksRes.data
      }
      url = id ? `${links[resource]}/${id}` : links[resource]
    }

    return axios({
      url,
      method,
      data,
      params,
      headers,
    })
  } catch (error) {
    console.error(error) // eslint-disable-line no-console
  }
}

@@ -173,20 +173,33 @@
 y %7B%0A    
-if (
+const basepath = 
 window.b
@@ -205,22 +205,24 @@
 basepath
-) %7B%0A  
+ %7C%7C ''%0A%0A
     url 
@@ -226,23 +226,16 @@
 rl = %60$%7B
-window.
 basepath
@@ -242,22 +242,16 @@
 %7D$%7Burl%7D%60
-%0A    %7D
 %0A%0A    if
@@ -451,16 +451,27 @@
 id ? %60$%7B
+basepath%7D$%7B
 links%5Bre
@@ -488,16 +488,30 @@
 %7Bid%7D%60 : 
+%60$%7Bbasepath%7D$%7B
 links%5Bre
@@ -517,16 +517,18 @@
 esource%5D
+%7D%60
 %0A    %7D%0A%0A

Fix basepath support.