<commit_before>
;(function() {

var root = this;

root.cartodb = root.cartodb || {};

var ramps = {
  green:  ['#EDF8FB', '#D7FAF4', '#CCECE6', '#66C2A4', '#41AE76', '#238B45', '#005824'],
  blue:  ['#FFFFCC', '#C7E9B4', '#7FCDBB', '#41B6C4', '#1D91C0', '#225EA8', '#0C2C84'],
  pink: ['#F1EEF6', '#D4B9DA', '#C994C7', '#DF65B0', '#E7298A', '#CE1256', '#91003F'],
  black:  ['#F7F7F7', '#D9D9D9', '#BDBDBD', '#969696', '#737373', '#525252', '#252525'],
  red:  ['#FFFFB2', '#FED976', '#FEB24C', '#FD8D3C', '#FC4E2A', '#E31A1C', '#B10026'],
  category: ['#A6CEE3', '#1F78B4', '#B2DF8A', '#33A02C', '#FB9A99', '#E31A1C', '#FDBF6F', '#FF7F00', '#CAB2D6', '#6A3D9A', '#DDDDDD']
};

function geoAttr(geometryType) {
  return {
    "line": 'line-color',
    'polygon': "polygon-fill",
    'point': "marker-fill"
  }[geometryType]
}

var CSS = {
  choropleth: function(quartiles, prop, geometryType, ramp) {
    var attr = geoAttr(geometryType);
    var css = "#c{ " + attr + ": #0C2C84; polygon-opacity: 0.6; line-color: #0C2C84; line-width: 0.0; line-opacity: 1; } "
    for(var i = quartiles.length - 1; i >= 0; --i) {
      if(quartiles[i] !== undefined && quartiles[i] != null) {
        css += "\n#c[ " + prop + " <= " + quartiles[i] + "] {\n";
        css += attr  + ":" + ramp[i] + ";\n}"
      }
    }
    return css;
  },

  category: function(cats, prop, geometryType) {
    var attr = geoAttr(geometryType);
    var ramp = ramps.cat;
    var css = "#c{ " + attr + ": #0C2C84; polygon-opacity: 0.6; line-color: #0C2C84; line-width: 0.0; line-opacity: 1; } "
    for(var i = cats.length - 1; i >= 0; --i) {
      if(cats[i] !== undefined && cats[i] != null) {
        css += "\n#c[ " + prop + " = '" + cats[i] + "'] {\n";
        css += attr  + ":" + ramp[i] + ";\n}"
      }
    }
    return css;
  }
}

//function columnMap(sql, c, geometryType, bbox) {
  //s.describe(sql, c, function(data) {
    //guessMap(sql, geometryType, data.column, data, bbox);
  //});
//}

function guessCss(sql, geometryType, column, stats) {
  var css = null
  if (stats.type == 'number') {
    css =  CSS.choropleth(stats.quantiles, column, geometryType, ramps.blue);
  } else if(stats.type == 'string') {
    css = CSS.category(stats.hist.slice(0, ramps.cat.length).map(function(r) { return r[0]; }), column, geometryType)
  }
  return css;
}

function guess(o, callback) {
  if (!callback) throw new Error("no callback");
  var s = cartodb.SQL({ user: o.user });
  s.describe(o.sql, 'the_geom', function(data) {
    var geometryType = data.simplified_geometry_type;
    s.describe(o.sql, o.column, function(data) {
      callback(
        null, 
        guessCss(o.sql, geometryType, data.column, data)
      )
    });
  })
}

function guessMap(sql, geometryType, column, stats, bbox) {
  var wizard = "choropleth";
  var css = null
  var mssg = "";
  var type = stats.type;

  if (stats.type == 'number') {
    if (['A','U'].indexOf(stats.dist_type) != -1) {
      // apply divergent scheme
      css = CSS.choropleth(stats.jenks, column, geometryType, ramps.divergent);
    } else if (stats.dist_type === 'F') {
      css = CSS.choropleth(stats.equalint, column, geometryType, ramps.blue);
    } else {
        if (stats.dist_type === 'J') {
          css = CSS.choropleth(stats.headtails, column, geometryType, ramps.blue);
        } else {
          var inverse_ramp = (_.clone(ramps.blue)).reverse();
          css = CSS.choropleth(stats.headtails, column, geometryType, inverse_ramp);
        }
    }
    
    mssg = '<span style="font-weight: bold; text-decoration: underline;">' + column + '</span>'
        + '<br /> std: ' + stats.stddev 
        + '<br /> avg: ' + stats.avg 
        + '<br /> num unique: ' + stats.distinct
        + '<br /> lstddev: ' + stats.lstddev
        + '<br /> count: ' + stats.count
        + '<br /> num nulls: ' + stats.null_ratio
        + '<br /> dist type: ' + stats.dist_type 
        + '<br /> weight: ' + stats.weight;
  
  } else if (stats.type == 'string') {
  
    mssg = '<span style="font-weight: bold; text-decoration: underline;">' + column + '</span>'
          + '<br />#uniques: ' + stats.distinct
          + '<br />null%: ' + stats.null_ratio
          + '<br />count: ' + stats.count
          + '<br />% in first 10 columns: ' + stats.skew
          + '<br />overall weight: ' +  stats.weight;
          wizard = "category";
          css = CSS.category(stats.hist.slice(0, ramps.cat.length).map(function(r) { return r[0]; }), column, geometryType);
  }

  if (css) {
    return {sql: sql, css: css, bbox: bbox, weight: stats.weight, type: type, mssg: mssg, wizard: wizard };
  } else {
    return {sql: sql, css: null, bbox: bbox, weight: -100, type: type, mssg: mssg, wizard: wizard};
  }
}
/*
CartoCSS.guess({
  user: '  '
  sql: '...'<commit_msg>adds metadata, modifications<commit_after>
;(function() {

var root = this;

root.cartodb = root.cartodb || {};

var ramps = {
  green:  ['#EDF8FB', '#D7FAF4', '#CCECE6', '#66C2A4', '#41AE76', '#238B45', '#005824'],
  blue:  ['#FFFFCC', '#C7E9B4', '#7FCDBB', '#41B6C4', '#1D91C0', '#225EA8', '#0C2C84'],
  pink: ['#F1EEF6', '#D4B9DA', '#C994C7', '#DF65B0', '#E7298A', '#CE1256', '#91003F'],
  black:  ['#F7F7F7', '#D9D9D9', '#BDBDBD', '#969696', '#737373', '#525252', '#252525'],
  red:  ['#FFFFB2', '#FED976', '#FEB24C', '#FD8D3C', '#FC4E2A', '#E31A1C', '#B10026'],
  category: ['#A6CEE3', '#1F78B4', '#B2DF8A', '#33A02C', '#FB9A99', '#E31A1C', '#FDBF6F', '#FF7F00', '#CAB2D6', '#6A3D9A', '#DDDDDD']
};

function geoAttr(geometryType) {
  return {
    "line": 'line-color',
    'polygon': "polygon-fill",
    'point': "marker-fill"
  }[geometryType]
}

var CSS = {
  choropleth: function(quartiles, tableName, prop, geometryType, ramp) {
    var attr = geoAttr(geometryType);
    var tableID = "#" + tableName;
    var css = "/** choropleth visualization */\n\n" + tableID + "{\n  " + attr + ": #0C2C84;\n  polygon-opacity: 0.6;\n  line-color: #0C2C84;\n  line-width: 0.0;\n  line-opacity: 1;\n} "
    for (var i = quartiles.length - 1; i >= 0; --i) {
      if (quartiles[i] !== undefined && quartiles[i] != null) {
        css += "\n" + tableID + "[" + prop + " <= " + quartiles[i] + "] {\n";
        css += "  " + attr  + ":" + ramp[i] + ";\n}"
      }
    }
    return css;
  },

  categoryMetadata: function(cats, prop, geometryType) {
    var metadata = [];

    for (var i = cats.length - 1; i >= 0; --i) {
      if (cats[i] !== undefined && cats[i] != null) {
        metadata.push({ title: cats[i], title_type: "string", value_type: 'color', color: ramps.category[i] });
      }
    }

    return metadata;
  },

  category: function(cats, tableName, prop, geometryType) {
    var attr = geoAttr(geometryType);
    var tableID = "#" + tableName;
    var ramp = ramps.category;
    var css = "/** category visualization */\n\n" + tableID + "{\n  " + attr + ": " + ramps.category[0] +";\n  line-color: #0C2C84;\n  line-opacity: 1; marker-fill-opacity: 0.9; marker-line-color: #FFF; marker-line-width: 1.5; marker-line-opacity: 1; marker-placement: point; marker-type: ellipse; marker-width: 10; marker-allow-overlap: true; \n}";

    for (var i = cats.length - 1; i >= 0; --i) {
      if (cats[i] !== undefined && cats[i] != null) {
        css += "\n" + tableID + "[" + prop + " = '" + cats[i] + "'] {\n";
        css += "  " + attr  + ":" + ramp[i] + ";\n}"
      }
    }
    return css;
  }
}

//function columnMap(sql, c, geometryType, bbox) {
  //s.describe(sql, c, function(data) {
    //guessMap(sql, geometryType, data.column, data, bbox);
  //});
//}

function guessCss(sql, geometryType, column, stats) {
  var css = null
  if (stats.type == 'number') {
    css =  CSS.choropleth(stats.quantiles, column, geometryType, ramps.blue);
  } else if(stats.type == 'string') {
    css = CSS.category(stats.hist.slice(0, ramps.cat.length).map(function(r) { return r[0]; }), column, geometryType)
  }
  return css;
}

function guess(o, callback) {
  if (!callback) throw new Error("no callback");
  var s = cartodb.SQL({ user: o.user });
  s.describe(o.sql, 'the_geom', function(data) {
    var geometryType = data.simplified_geometry_type;
    s.describe(o.sql, o.column, function(data) {
      callback(
        null, 
        guessCss(o.sql, geometryType, data.column, data)
      )
    });
  })
}

function guessMap(sql, tableName, column, stats) {
  var geometryType = column.get("geometry_type");
    var bbox =  column.get("bbox");
    var columnName = column.get("name");
    var wizard = "choropleth";
    var css = null
    var type = stats.type;
    var metadata = []

  if (stats.type == 'number') {
    if (['A','U'].indexOf(stats.dist_type) != -1) {
      // apply divergent scheme
      css = CSS.choropleth(stats.jenks, tableName, column, geometryType, ramps.divergent);
    } else if (stats.dist_type === 'F') {
      css = CSS.choropleth(stats.equalint, tableName, column, geometryType, ramps.blue);
    } else {
      if (stats.dist_type === 'J') {
        css = CSS.choropleth(stats.headtails, tableName, column, geometryType, ramps.blue);
      } else {
        var inverse_ramp = (_.clone(ramps.blue)).reverse();
        css = CSS.choropleth(stats.headtails, tableName, column, geometryType, inverse_ramp);
      }
    }
  
  } else if (stats.type == 'string') {

      wizard   = "category";
      css      = CSS.category(stats.hist.slice(0, ramps.category.length).map(function(r) { return r[0]; }),tableName, columnName, geometryType);
      metadata = CSS.categoryMetadata(stats.hist.slice(0, ramps.category.length).map(function(r) { return r[0]; }),tableName, columnName, geometryType);

      var wizard_properties = {
        "marker-fill-opacity": 0.9,
        "marker-line-color": "#FFFFFF",
        "marker-fill": ramps.category[0],
        "marker-line-width": 1.5,
        "marker-line-opacity": 1,
        "marker-placement": "point",
        "marker-type": "ellipse",
        "marker-width": 10,
        "marker-allow-overlap": true
      };

    }

  if (css) {
    return { sql: sql, css: css, metadata: metadata, wizard_properties: wizard_properties, column: columnName, bbox: bbox, stats: stats, type: type, wizard: wizard  };
  } else {
    return { sql: sql, css: null, metadata: metadata, column: columnName, bbox: bbox, weight: -100, type: type, wizard: wizard };
  }
}
/*
CartoCSS.guess({
  user: '  '
  sql: '...'
{'fraction_non_alphanumeric': 0.20397529911231185, 'fraction_numerical': 0.03608645310690853, 'mean_word_length': 2.9727865082407052, 'pattern_counts': {'":': 19, '<': 22, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7107705', 'n_tokens_mistral': 4110, 'n_tokens_neox': 3889, 'n_words': 853}