const cartoUser = 'cpp';
const cartoDomain = 'cartoprod.capitalplanning.nyc';

const buildTemplate = (layergroupid) => { // eslint-disable-line
  return `https://${cartoDomain}/user/${cartoUser}/api/v1/map/${layergroupid}/{z}/{x}/{y}.png`;
};

const carto = {
  SQL(query, type = 'json') {
    const cleanedQuery = query.replace('\n', '');
    return new Promise((resolve, reject) => {
      $.ajax({
        type: 'GET',
        url: `https://${cartoDomain}/user/${cartoUser}/api/v2/sql?q=${cleanedQuery}&format=${type}`,
        success(d) {
          resolve(type === 'json' ? d.rows : d);
        },
      })
        .fail(() => reject());
    });
  },

  getTileTemplate() {
    const SQL = `
      SELECT a.the_geom_webmercator, b.code as landuse, b.description as landuse_desc
      FROM support_mappluto a
      LEFT JOIN support_landuse_lookup b
      ON a.landuse::integer = b.code
    `;

    const CartoCSS = `
      #pluto15v1 {
       polygon-opacity: 0.8;
       line-width: .5;
       line-opacity: 1;
       polygon-fill: #000;
      }

      #pluto15v1[landuse=1] {
       polygon-fill: #f4f455;
       line-color: #f4f455;
      }
      #pluto15v1[landuse=2] {
       polygon-fill: #f7d496;
       line-color: #f7d496;
      }
      #pluto15v1[landuse=3] {
       polygon-fill: #FF9900;
       line-color: #FF9900;
      }
      #pluto15v1[landuse=4] {
       polygon-fill: #f7cabf;
      line-color: #f7cabf;
      }
      #pluto15v1[landuse=5] {
       polygon-fill: #ea6661;
      line-color: #ea6661;
      }
      #pluto15v1[landuse=6] {
       polygon-fill: #d36ff4;
      line-color: #d36ff4;
      }
      #pluto15v1[landuse=7] {
       polygon-fill: #dac0e8;
      line-color: #dac0e8;
      }
      #pluto15v1[landuse=8] {
       polygon-fill: #5CA2D1;
      line-color: #5CA2D1;
      }
      #pluto15v1[landuse=9] {
       polygon-fill: #8ece7c;
      line-color: #8ece7c;
      }
      #pluto15v1[landuse=10] {
       polygon-fill: #bab8b6;
      line-color: #bab8b6;
      }

      #pluto15v1[landuse=11] {
       polygon-fill: #5f5f60;
      line-color: #5f5f60;
      }
    `;

    const params = {
      version: '1.3.0',
      layers: [{
        type: 'mapnik',
        options: {
          cartocss_version: '2.1.1',
          cartocss: CartoCSS,
          sql: SQL,
        },
      }],
    };

    return new Promise((resolve, reject) => {
      fetch(`https://${cartoDomain}/user/${cartoUser}/api/v1/map`, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify(params),
      })
        .catch(err => reject(err))
        .then(response => response.json())
        .then((json) => { resolve(buildTemplate(json.layergroupid)); });
    });
  },
};

export default carto;

@@ -237,16 +237,170 @@
 g%60;%0A%7D;%0A%0A
+const buildSqlUrl = (cleanedQuery, type = 'json') =%3E %7B%0A  return %60https://$%7BcartoDomain%7D/user/$%7BcartoUser%7D/api/v2/sql?q=$%7BcleanedQuery%7D&format=$%7Btype%7D%60%0A%7D%0A%0A
 const ca
@@ -586,65 +586,20 @@
 rl: 
-%60https://$%7BcartoDomain%7D/user/$%7BcartoUser%7D/api/v2/sql?q=$%7B
+buildSqlUrl(
 clea
@@ -606,33 +606,23 @@
 nedQuery
-%7D&format=$%7B
+, 
 type
-%7D%60
+)
 ,%0A      
@@ -2856,16 +2856,40 @@
  %7D,%0A%7D;%0A%0A
+export %7B buildSqlUrl %7D;%0A
 export d

Allow for more modularity in util