function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import React from "react";
import { ScatterplotLayer } from "deck.gl";
import { number, string, bool, func, arrayOf, shape } from "prop-types";
import shortid from "shortid";
import centerOfMass from "@turf/center-of-mass";
import { createColorScale, updateQuantileScale, updateEqualScale, createSizeScale } from "./createLayers";

var MultiScatterPlotMap = function MultiScatterPlotMap(props) {
  var id = props.id,
      data = props.data,
      _props$pickable = props.pickable,
      pickable = _props$pickable === void 0 ? true : _props$pickable,
      _props$civicColor = props.civicColor,
      civicColor = _props$civicColor === void 0 ? "civicYellow" : _props$civicColor,
      _props$opacity = props.opacity,
      opacity = _props$opacity === void 0 ? 0.7 : _props$opacity,
      _props$getPosition = props.getPosition,
      getPosition = _props$getPosition === void 0 ? function (f) {
    return f.geometry.coordinates;
  } : _props$getPosition,
      _props$radius = props.radius,
      radius = _props$radius === void 0 ? 25 : _props$radius,
      _props$radiusScale = props.radiusScale,
      radiusScale = _props$radiusScale === void 0 ? 1 : _props$radiusScale,
      _props$autoHighlight = props.autoHighlight,
      autoHighlight = _props$autoHighlight === void 0 ? true : _props$autoHighlight,
      _props$highlightColor = props.highlightColor,
      highlightColor = _props$highlightColor === void 0 ? [255, 255, 0, 100] : _props$highlightColor,
      onHoverSlide = props.onHoverSlide,
      onLayerClick = props.onLayerClick,
      _props$scaleType = props.scaleType,
      scaleType = _props$scaleType === void 0 ? {} : _props$scaleType,
      _props$fieldName = props.fieldName,
      fieldName = _props$fieldName === void 0 ? {} : _props$fieldName,
      _props$dataRange = props.dataRange,
      dataRange = _props$dataRange === void 0 ? [] : _props$dataRange,
      _props$colorRange = props.colorRange,
      colorRange = _props$colorRange === void 0 ? [] : _props$colorRange,
      index = props.index;
  var colorScale = createColorScale(civicColor, scaleType, dataRange, colorRange);

  if (scaleType.color === "equal") {
    colorScale = updateEqualScale(data, colorScale, civicColor, dataRange, colorRange, fieldName);
  }

  if (scaleType.color === "quantile") {
    colorScale = updateQuantileScale(data, colorScale, civicColor, colorRange, fieldName);
  }

  var getColor = function getColor(feature) {
    var fieldNameColor = fieldName.color;

    if (fieldName && fieldNameColor) {
      var value = feature.properties[fieldNameColor];
      return value ? colorScale(value) : [0, 0, 0, 128];
    }

    return colorScale();
  };

  var memoizedData = React.useMemo(function () {
    if (data.length === 0) {
      return [];
    }

    return data[0].geometry.type === "Polygon" || data[0].geometry.type === "MultiPolygon" ? data.map(function (d) {
      return centerOfMass(d.geometry, {
        properties: _objectSpread({}, d.properties)
      });
    }).filter(function (d) {
      return d.geometry;
    }) : data;
  }, [data]);
  var getRadius = createSizeScale(radius, scaleType, fieldName);
  var scaleTypeArea = scaleType.area;
  var circleScale = scaleTypeArea ? radiusScale : 1;
  return React.createElement(ScatterplotLayer, {
    key: shortid.generate(),
    id: id,
    pickable: pickable,
    data: memoizedData,
    getPosition: getPosition,
    opacity: opacity,
    getFillColor: getColor,
    getRadius: getRadius,
    radiusScale: circleScale,
    radiusMinPixels: 1,
    autoHighlight: autoHighlight,
    highlightColor: highlightColor,
    onHover: function onHover(info) {
      return onHoverSlide(info, index);
    },
    onClick: onLayerClick,
    updateTriggers: {
      getColor: getColor,
      getRadius: getRadius
    },
    parameters: {
      depthTest: false
    }
  });
};

MultiScatterPlotMap.propTypes = {
  id: string.isRequired,
  data: arrayOf(shape({})).isRequired,
  pickable: bool,
  civicColor: string,
  opacity: number,
  getPosition: func,
  radius: number,
  radiusScale: number,
  autoHighlight: bool,
  highlightColor: arrayOf(number),
  onHoverSlide: func,
  onLayerClick: func,
  scaleType: shape({
    area: string,
    color: string
  }),
  fieldName: shape({
    area: string,
    color: string
  }),
  dataRange: arrayOf(string),
  colorRange: arrayOf(arrayOf(number)),
  index: number
};
export default MultiScatterPlotMap;