function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

import { scaleQuantize, scaleThreshold, scaleOrdinal, scaleQuantile, extent, color as d3Color } from "d3";
import CIVIC_MAP_COLORS from "./mapStyles";
var white = "#fff";

var convertToRGB = function convertToRGB(colorArray) {
  return colorArray.map(function (d) {
    return d3Color(d) ? d3Color(d) : d3Color(white);
  }).map(function (d) {
    return [d.r, d.g, d.b];
  });
};

var createRange = function createRange(civicColor, colorRange) {
  if (colorRange.length) {
    if (d3Color(colorRange[0])) {
      return convertToRGB(colorRange);
    }

    return colorRange;
  }

  if (civicColor && CIVIC_MAP_COLORS[civicColor]) {
    return CIVIC_MAP_COLORS[civicColor];
  }

  return CIVIC_MAP_COLORS.earth;
};

var createEqualScale = function createEqualScale() {
  return scaleQuantize();
};

export var updateEqualScale = function updateEqualScale(featuresData, equalScale, civicColorSelection, equalDomain, equalRange, field) {
  var colorFieldName = field.color;
  var domain = equalDomain.length ? equalDomain : extent(featuresData, function (d) {
    return +d.properties[colorFieldName];
  });
  var range = createRange(civicColorSelection, equalRange);
  return equalScale.domain(domain).range(range).nice();
};

var createQuantileScale = function createQuantileScale() {
  return scaleQuantile();
};

export var updateQuantileScale = function updateQuantileScale(featuresData, quantileScale, civicColorSelection, quantileRange, field) {
  var colorFieldName = field.color;
  var domain = featuresData.map(function (d) {
    return +d.properties[colorFieldName];
  }).reduce(function (a, c) {
    return a.indexOf(c) === -1 ? [].concat(_toConsumableArray(a), [c]) : a;
  }, []);
  var colorRange = quantileRange.length ? quantileRange : CIVIC_MAP_COLORS[civicColorSelection] ? CIVIC_MAP_COLORS[civicColorSelection] : CIVIC_MAP_COLORS.earth;
  return quantileScale.domain(domain).range(colorRange);
};

var createThresholdScale = function createThresholdScale(civicColorSelection, categories, customColors) {
  var range = createRange(civicColorSelection, customColors);
  return scaleThreshold().domain(categories).range(range);
};

var createDiscreteScale = function createDiscreteScale(categories, colorRange) {
  var range = createRange("", colorRange);
  return scaleOrdinal().domain(categories).range(range).unknown([255, 255, 255, 128]);
};

export var createColorScale = function createColorScale(colorSelection) {
  var scaleType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var dataRange = arguments.length > 2 ? arguments[2] : undefined;
  var colorRange = arguments.length > 3 ? arguments[3] : undefined;
  var colorScaleType = scaleType.color;

  if (scaleType && colorScaleType === "equal") {
    return createEqualScale();
  }

  if (scaleType && colorScaleType === "quantile") {
    return createQuantileScale();
  }

  if (scaleType && colorScaleType === "threshold") {
    return createThresholdScale(colorSelection, dataRange, colorRange);
  }

  if (scaleType && colorScaleType === "ordinal") {
    return createDiscreteScale(dataRange, colorRange);
  }

  return function () {
    return CIVIC_MAP_COLORS[colorSelection];
  };
};
export var createSizeScale = function createSizeScale(width) {
  var scaleType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var fieldName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  var areaScaleType = scaleType.area;
  var areaFieldName = fieldName.area;

  if (scaleType && areaScaleType === "circle area") {
    return function (feature) {
      return Math.sqrt(feature.properties[areaFieldName] / Math.PI);
    };
  }

  return function () {
    return width;
  };
};