function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

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

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

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

function _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _nonIterableRest(); }

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

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

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

import React from "react";
import { Source, Layer } from "react-map-gl";
import { string, shape, number, arrayOf, oneOfType } from "prop-types";
var VectorTilesMap = React.memo(function (props) {
  var vectorTilesID = props.vectorTilesID,
      vectorTilesURL = props.vectorTilesURL,
      layerID = props.layerID,
      layerType = props.layerType,
      sourceLayer = props.sourceLayer,
      paint = props.paint,
      layerPosition = props.layerPosition,
      index = props.index,
      multipleLayers = props.multipleLayers,
      filter = props.filter;
  var sourceLayerProp = {
    "source-layer": sourceLayer
  };
  var metaProps = {
    "sandbox:index": index
  };
  var greaterThan = /&gt;/;
  var lessThan = /&lt;/;

  var formatFilter = function formatFilter(arr) {
    var _arr = _toArray(arr),
        filterKey = _arr[0],
        rest = _arr.slice(1);

    if (filterKey.search(greaterThan) > -1) {
      return [filterKey.replace(greaterThan, ">")].concat(_toConsumableArray(rest));
    }

    if (filterKey.search(lessThan) > -1) {
      return [filterKey.replace(lessThan, "<")].concat(_toConsumableArray(rest));
    }

    return arr;
  };

  var showMultiLayers = multipleLayers.length > 0 && multipleLayers.map(function (layer) {
    var multiLayerPosition = layer.layerPosition,
        multiSourceLayer = layer.sourceLayer,
        multiLayerID = layer.layerID,
        multiLayerType = layer.layerType,
        multiPaint = layer.paint,
        _layer$hover = layer.hover,
        hover = _layer$hover === void 0 ? false : _layer$hover;
    var multiSourceLayerProp = {
      "source-layer": multiSourceLayer
    };
    var filterProp = filter.length > 0 && hover ? {
      filter: formatFilter(filter)
    } : {};
    return React.createElement(Layer, _extends({
      beforeId: multiLayerPosition,
      id: multiLayerID,
      key: multiLayerID,
      type: multiLayerType,
      source: vectorTilesID + index,
      paint: multiPaint,
      metadata: metaProps
    }, multiSourceLayerProp, filterProp));
  });
  var filterProp = filter.length > 0 ? {
    filter: formatFilter(filter)
  } : {};
  return React.createElement(Source, {
    type: "vector",
    id: vectorTilesID + index,
    url: vectorTilesURL
  }, showMultiLayers || React.createElement(Layer, _extends({
    beforeId: layerPosition,
    id: layerID,
    type: layerType,
    source: vectorTilesID + index,
    paint: paint
  }, sourceLayerProp, {
    metadata: metaProps
  }, filterProp)));
});
VectorTilesMap.propTypes = {
  vectorTilesURL: string,
  vectorTilesID: string,
  layerID: string,
  layerType: string,
  sourceLayer: string,
  paint: shape({}),
  layerPosition: string,
  index: number,
  filter: arrayOf(oneOfType([string, number])),
  multipleLayers: arrayOf(shape({
    layerID: string,
    layerType: string,
    sourceLayer: string,
    paint: shape({})
  }))
};
VectorTilesMap.defaultProps = {
  multipleLayers: [],
  filter: []
};
export default VectorTilesMap;