"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireDefault(require("react"));

var _deck = require("deck.gl");

var _propTypes = require("prop-types");

var _shortid = _interopRequireDefault(require("shortid"));

var _centerOfMass = _interopRequireDefault(require("@turf/center-of-mass"));

var _createLayers = require("./createLayers");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

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; }

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 = (0, _createLayers.createColorScale)(civicColor, scaleType, dataRange, colorRange);

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

  if (scaleType.color === "quantile") {
    colorScale = (0, _createLayers.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["default"].useMemo(function () {
    if (data.length === 0) {
      return [];
    }

    return data[0].geometry.type === "Polygon" || data[0].geometry.type === "MultiPolygon" ? data.map(function (d) {
      return (0, _centerOfMass["default"])(d.geometry, {
        properties: _objectSpread({}, d.properties)
      });
    }).filter(function (d) {
      return d.geometry;
    }) : data;
  }, [data]);

  var getRadius = (0, _createLayers.createSizeScale)(radius, scaleType, fieldName);
  var scaleTypeArea = scaleType.area;
  var circleScale = scaleTypeArea ? radiusScale : 1;
  return _react["default"].createElement(_deck.ScatterplotLayer, {
    key: _shortid["default"].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: _propTypes.string.isRequired,
  data: (0, _propTypes.arrayOf)((0, _propTypes.shape)({})).isRequired,
  pickable: _propTypes.bool,
  civicColor: _propTypes.string,
  opacity: _propTypes.number,
  getPosition: _propTypes.func,
  radius: _propTypes.number,
  radiusScale: _propTypes.number,
  autoHighlight: _propTypes.bool,
  highlightColor: (0, _propTypes.arrayOf)(_propTypes.number),
  onHoverSlide: _propTypes.func,
  onLayerClick: _propTypes.func,
  scaleType: (0, _propTypes.shape)({
    area: _propTypes.string,
    color: _propTypes.string
  }),
  fieldName: (0, _propTypes.shape)({
    area: _propTypes.string,
    color: _propTypes.string
  }),
  dataRange: (0, _propTypes.arrayOf)(_propTypes.string),
  colorRange: (0, _propTypes.arrayOf)((0, _propTypes.arrayOf)(_propTypes.number)),
  index: _propTypes.number
};
var _default = MultiScatterPlotMap;
exports["default"] = _default;