import React from "react";
import { IconLayer } from "deck.gl";
import shortid from "shortid";
import { number, string, bool, func, arrayOf, shape } from "prop-types";
import { createColorScale, createSizeScale } from "./createLayers";

var getIconSizeScale = function getIconSizeScale(zoom) {
  return zoom > 11.5 ? 6 : zoom > 10.5 ? 5 : zoom > 9.5 ? 4 : zoom > 8.5 ? 3 : zoom > 7.5 ? 2 : 1;
};

var MultiIconMap = function MultiIconMap(props) {
  var id = props.id,
      data = props.data,
      _props$pickable = props.pickable,
      pickable = _props$pickable === void 0 ? true : _props$pickable,
      _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,
      iconAtlas = props.iconAtlas,
      iconMapping = props.iconMapping,
      _props$getSizeScale = props.getSizeScale,
      getSizeScale = _props$getSizeScale === void 0 ? getIconSizeScale : _props$getSizeScale,
      _props$getIcon = props.getIcon,
      getIcon = _props$getIcon === void 0 ? function (f) {
    return f.properties.type;
  } : _props$getIcon,
      _props$iconSize = props.iconSize,
      iconSize = _props$iconSize === void 0 ? 10 : _props$iconSize,
      _props$autoHighlight = props.autoHighlight,
      autoHighlight = _props$autoHighlight === void 0 ? true : _props$autoHighlight,
      _props$highlightColor = props.highlightColor,
      highlightColor = _props$highlightColor === void 0 ? [0, 0, 128, 128] : _props$highlightColor,
      onHoverSlide = props.onHoverSlide,
      onLayerClick = props.onLayerClick,
      viewport = props.viewport,
      _props$civicColor = props.civicColor,
      civicColor = _props$civicColor === void 0 ? "civicYellow" : _props$civicColor,
      scaleType = props.scaleType,
      _props$fieldName = props.fieldName,
      fieldName = _props$fieldName === void 0 ? {} : _props$fieldName,
      dataRange = props.dataRange,
      colorRange = props.colorRange;
  var colorScale = createColorScale(civicColor, scaleType, dataRange, colorRange);

  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 getSize = createSizeScale(iconSize, scaleType, fieldName);
  var zoom = viewport.zoom;
  var sizeScale = getSizeScale(zoom);
  return React.createElement(IconLayer, {
    key: shortid.generate(),
    id: id,
    data: data,
    pickable: pickable,
    opacity: opacity,
    getPosition: getPosition,
    iconAtlas: iconAtlas,
    iconMapping: iconMapping,
    sizeScale: sizeScale,
    getIcon: getIcon,
    getSize: getSize,
    getColor: getColor,
    autoHighlight: autoHighlight,
    highlightColor: highlightColor,
    onHover: onHoverSlide,
    onClick: onLayerClick
  });
};

MultiIconMap.propTypes = {
  id: string.isRequired,
  data: arrayOf(shape({})).isRequired,
  pickable: bool,
  civicColor: string,
  opacity: number,
  getPosition: func,
  iconAtlas: string,
  iconMapping: shape({
    x: number,
    y: number,
    width: number,
    height: number,
    mask: bool
  }),
  getSizeScale: func,
  getIcon: func,
  iconSize: 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)),
  viewport: shape({})
};
export default MultiIconMap;