"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TDBGeoCollectionDocuments = void 0;
var _react = _interopRequireWildcard(require("react"));
var _Stack = _interopRequireDefault(require("react-bootstrap/Stack"));
var _reactLeafletFullscreen = require("react-leaflet-fullscreen");
require("react-leaflet-fullscreen/dist/styles.css");
var _LabelComponent = require("../components/LabelComponent");
var _reactLeaflet = require("react-leaflet");
var CONST = _interopRequireWildcard(require("../constants"));
var _map = require("./map.constants");
var util = _interopRequireWildcard(require("../utils"));
var _markers = require("./markers");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function getMapVectors(formData) {
  var mapVectors = [],
    lineColor = {
      color: _map.LINE_COLOR
    };
  formData.map(function (geo) {
    if (geo.hasOwnProperty(CONST.TYPE) && geo[CONST.TYPE] === CONST.POINT && geo.hasOwnProperty(CONST.COORDINATES_FIELD)) {
      // POINTS 
      mapVectors.push( /*#__PURE__*/_react["default"].createElement(_reactLeaflet.Marker, {
        position: geo[CONST.COORDINATES_FIELD],
        icon: CONST["default"]
      }, /*#__PURE__*/_react["default"].createElement(_reactLeaflet.Popup, null, /*#__PURE__*/_react["default"].createElement("span", null, "lat: ".concat(geo[CONST.COORDINATES_FIELD][0], ", lng: ").concat(geo[CONST.COORDINATES_FIELD][1])))));
    } else if (geo.hasOwnProperty(CONST.TYPE) && geo[CONST.TYPE] === CONST.LINE_STRING_TYPE && geo.hasOwnProperty(CONST.COORDINATES_FIELD)) {
      // LINE STRING 
      mapVectors.push( /*#__PURE__*/_react["default"].createElement(_reactLeaflet.Polyline, {
        pathOptions: lineColor,
        positions: geo[CONST.COORDINATES_FIELD]
      }));
    } else if (geo.hasOwnProperty(CONST.TYPE) && geo[CONST.TYPE] === CONST.POLYGON && geo.hasOwnProperty(CONST.COORDINATES_FIELD)) {
      // POLYGON
      mapVectors.push( /*#__PURE__*/_react["default"].createElement(_reactLeaflet.Polygon, {
        pathOptions: lineColor,
        positions: geo[CONST.COORDINATES_FIELD]
      }));
    } else if (geo.hasOwnProperty(CONST.TYPE) && geo[CONST.TYPE] === CONST.MULTIPOLYGON && geo.hasOwnProperty(CONST.COORDINATES_FIELD)) {
      // MULTIPOLYGON
      mapVectors.push( /*#__PURE__*/_react["default"].createElement(_reactLeaflet.Polygon, {
        pathOptions: lineColor,
        positions: geo[CONST.COORDINATES_FIELD]
      }));
    } else if (geo.hasOwnProperty(CONST.TYPE) && geo[CONST.TYPE] === CONST.GEOMETRY_COLLECTION && geo.hasOwnProperty(CONST.GEOMETRIES)) {
      // GEOMETRIES
      var mapItems = getMapVectors(geo[CONST.GEOMETRIES]);
      mapVectors.push(mapItems);
    }
  });
  return mapVectors;
}
var TDBGeoCollectionDocuments = function TDBGeoCollectionDocuments(_ref) {
  var config = _ref.config;
  // when nothing is filled return blank div
  if (!config.formData.hasOwnProperty(config.name)) return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null);
  var center = (0, _markers.getCenterFromData)(config.formData[config.name]);
  var zoom = _map.MAP_OPTION.zoom;
  var bounds = util.setBounds(config.formData.hasOwnProperty(CONST.B_BOX) ? config.formData[CONST.B_BOX] : []);
  var mapVectors = getMapVectors(config.formData[config.name]);
  return /*#__PURE__*/_react["default"].createElement(_Stack["default"], {
    direction: "horizontal",
    className: "mb-3"
  }, /*#__PURE__*/_react["default"].createElement(_LabelComponent.TDBLabel, {
    name: config.label ? config.label : config.name,
    required: config.required,
    id: config.id
  }), /*#__PURE__*/_react["default"].createElement(_reactLeaflet.MapContainer, {
    center: center,
    zoom: zoom,
    scrollWheelZoom: true,
    bounds: bounds,
    className: "rounded"
  }, /*#__PURE__*/_react["default"].createElement(_reactLeaflet.TileLayer, {
    attribution: "\xA9 <a href=\"https://www.openstreetmap.org/copyright\">OpenStreetMap</a> contributors",
    url: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
  }), mapVectors, /*#__PURE__*/_react["default"].createElement(_reactLeafletFullscreen.FullscreenControl, {
    forceSeparateButton: true
  })));
};
exports.TDBGeoCollectionDocuments = TDBGeoCollectionDocuments;