import AJAX from 'utils/ajax';
import {buildInfluxUrl, proxy} from 'utils/queryUrlGenerator';

export function showDatabases(source) {
  const query = `SHOW DATABASES`;

  return proxy({source, query});
}

export function showQueries(host, db, clusterID) {
  const statement = 'SHOW QUERIES';
  const url = buildInfluxUrl({host, statement, database: db});

  return proxy(url, clusterID);
}

export function killQuery(host, queryId, clusterID) {
  const statement = `KILL QUERY ${queryId}`;
  const url = buildInfluxUrl({host, statement});

  return proxy(url, clusterID);
}

export function showMeasurements(source, db) {
  const query = 'SHOW MEASUREMENTS';

  return proxy({source, db, query});
}

export function showTagKeys({source, database, retentionPolicy, measurement}) {
  const query = `SHOW TAG KEYS FROM "${measurement}"`;

  return proxy({source, db: database, rp: retentionPolicy, query});
}

export function showTagValues({source, database, retentionPolicy, measurement, tagKeys}) {
  const keys = tagKeys.sort().map((k) => `"${k}"`).join(', ');
  const query = `SHOW TAG VALUES FROM "${measurement}" WITH KEY IN (${keys})`;

  return proxy({source, db: database, rp: retentionPolicy, query});
}

export function showRetentionPolicies(source, databases) {
  let query;
  if (Array.isArray(databases)) {
    query = databases.map((db) => `SHOW RETENTION POLICIES ON "${db}"`).join(';');
  } else {
    query = `SHOW RETENTION POLICIES ON "${databases}"`;
  }

  return proxy({source, query});
}

export function showShards() {
  return AJAX({
    url: `/api/int/v1/show-shards`,
  });
}

export function createRetentionPolicy({host, database, rpName, duration, replicationFactor, clusterID}) {
  const statement = `CREATE RETENTION POLICY "${rpName}" ON "${database}" DURATION ${duration} REPLICATION ${replicationFactor}`;
  const url = buildInfluxUrl({host, statement});

  return proxy(url, clusterID);
}

export function dropShard(host, shard, clusterID) {
  const statement = `DROP SHARD ${shard.shardId}`;
  const url = buildInfluxUrl({host, statement});

  return proxy(url, clusterID);
}

export function showFieldKeys(source, db, measurement, rp) {
  const query = `SHOW FIELD KEYS FROM "${rp}"."${measurement}"`;

  return proxy({source, query, db});
}

@@ -231,27 +231,18 @@
 ies(
-host, db, clusterID
+source, db
 ) %7B%0A
@@ -249,25 +249,21 @@
   const 
-statement
+query
  = 'SHOW
@@ -277,101 +277,43 @@
 S';%0A
-  const url = buildInfluxUrl(%7Bhost, statement, database: db%7D);%0A%0A  return proxy(url, clusterID
+%0A  return proxy(%7Bsource, query, db%7D
 );%0A%7D
@@ -340,20 +340,22 @@
 llQuery(
-host
+source
 , queryI
@@ -355,27 +355,16 @@
  queryId
-, clusterID
 ) %7B%0A  co
@@ -363,33 +363,29 @@
 ) %7B%0A  const 
-statement
+query
  = %60KILL QUE
@@ -403,57 +403,8 @@
 d%7D%60;
-%0A  const url = buildInfluxUrl(%7Bhost, statement%7D);
 %0A%0A  
@@ -412,38 +412,39 @@
 eturn proxy(
-url, clusterID
+%7Bsource, query%7D
 );%0A%7D%0A%0Aexport

Update metaQueries to use new API