"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _layer = require("ol/layer");
var _source = require("ol/source");
var _map = _interopRequireDefault(require("../map"));
var _featureDraw = _interopRequireDefault(require("../feature-draw"));
var _VtxModal = _interopRequireDefault(require("./VtxModal"));
var _button = _interopRequireDefault(require("antd/lib/button"));
var _location = _interopRequireDefault(require("../assets/location.png"));
var _noLocation = _interopRequireDefault(require("../assets/noLocation.png"));
var _icons = require("@ant-design/icons");
var _ol = require("ol");
var _geom = require("ol/geom");
var _Marker = _interopRequireDefault(require("../marker/Marker"));
var _Polygon = _interopRequireDefault(require("../polygon/Polygon"));
var _Circle = _interopRequireDefault(require("../circle/Circle"));
var _Polyline = _interopRequireDefault(require("../polyline/Polyline"));
var _Search = _interopRequireDefault(require("../poi-search/Search"));
var _PoiList = _interopRequireDefault(require("../poi-search/PoiList"));
var _FeatureLayer = _interopRequireDefault(require("../feature-layer/FeatureLayer"));
var _search = require("../_util/search");
var _style = require("ol/style");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
var styles = {
  searchModal: 'vtx-ui-searchmap-searchmodal',
  searchMap: 'vtx-ui-searchmap-searchmap',
  top: 'vtx-ui-searchmap-top',
  bottom: 'vtx-ui-searchmap-bottom',
  content: 'vtx-ui-searchmap-content',
  content_right: 'vtx-ui-searchmap-content_right'
};
var _default = exports["default"] = function _default(props) {
  var mapRef = (0, _react.useRef)();
  var _props$mapCenter = props.mapCenter,
    mapCenter = _props$mapCenter === void 0 ? [116.391349, 39.907375] : _props$mapCenter,
    _props$modalVisible = props.modalVisible,
    modalVisible = _props$modalVisible === void 0 ? false : _props$modalVisible,
    graphicType = props.graphicType,
    closeModal = props.closeModal,
    callback = props.callback,
    callbackDrawEnd = props.callbackDrawEnd,
    editParam = props.editParam,
    drawParameter = props.drawParameter,
    _props$limitAreaLngLa = props.limitAreaLngLat,
    limitAreaLngLat = _props$limitAreaLngLa === void 0 ? [] : _props$limitAreaLngLa,
    _props$limitAreaZInde = props.limitAreaZIndex,
    limitAreaZIndex = _props$limitAreaZInde === void 0 ? 1 : _props$limitAreaZInde,
    _props$limitAreaFeatu = props.limitAreaFeature,
    limitAreaFeature = _props$limitAreaFeatu === void 0 ? null : _props$limitAreaFeatu,
    _props$onCreated = props.onCreated,
    onCreated = _props$onCreated === void 0 ? function () {} : _props$onCreated,
    _props$isChina = props.isChina,
    isChina = _props$isChina === void 0 ? false : _props$isChina;
  var _useState = (0, _react.useState)(null),
    _useState2 = _slicedToArray(_useState, 2),
    feature = _useState2[0],
    setFeature = _useState2[1];
  var _useState3 = (0, _react.useState)([]),
    _useState4 = _slicedToArray(_useState3, 2),
    callbackData = _useState4[0],
    setCallbackData = _useState4[1];
  var _useState5 = (0, _react.useState)(0),
    _useState6 = _slicedToArray(_useState5, 2),
    refresh = _useState6[0],
    setRefresh = _useState6[1];
  var _useState7 = (0, _react.useState)(graphicType),
    _useState8 = _slicedToArray(_useState7, 2),
    drawType = _useState8[0],
    setDrawType = _useState8[1];
  var _useState9 = (0, _react.useState)({}),
    _useState10 = _slicedToArray(_useState9, 2),
    poi = _useState10[0],
    setPoi = _useState10[1];
  var _useState11 = (0, _react.useState)(0),
    _useState12 = _slicedToArray(_useState11, 2),
    clearFlag = _useState12[0],
    setClearFlag = _useState12[1];
  var _useState13 = (0, _react.useState)(null),
    _useState14 = _slicedToArray(_useState13, 2),
    constrainGeom = _useState14[0],
    setConstrainGeom = _useState14[1];
  // 绘制区域限制
  var source = new _source.Vector();
  var vector = new _layer.Vector({
    source: source,
    zIndex: limitAreaZIndex,
    style: new _style.Style({
      fill: new _style.Fill({
        color: 'rgba(0,128,255, 0.3)'
      }),
      stroke: new _style.Stroke({
        color: 'rgba(0,128,255, 0.3)',
        width: 2
      })
    })
  });
  (0, _react.useEffect)(function () {
    setDrawType(graphicType);
  }, [graphicType]);
  (0, _react.useEffect)(function () {
    if (mapRef !== null && mapRef !== void 0 && mapRef.current && graphicType && editParam) {
      switch (graphicType) {
        case 'Marker':
          var featureA = new _ol.Feature();
          var geometryA = new _geom.Point(editParam);
          setCallbackData(editParam);
          featureA.setGeometry(geometryA);
          featureA.setStyle(new _Marker["default"]({
            image: _location["default"],
            scale: 1
          }));
          setFeature(featureA);
          break;
        case 'DragBox':
        case 'Polygon':
          var featureB = new _ol.Feature();
          var geometryB = new _geom.Polygon(editParam);
          setCallbackData(editParam);
          featureB.setGeometry(geometryB);
          if (drawParameter) {
            featureB.setStyle(new _Polygon["default"](drawParameter));
          }
          setFeature(featureB);
          break;
        case 'Circle':
          var featureC = new _ol.Feature();
          if ((editParam === null || editParam === void 0 ? void 0 : editParam.length) === 3) {
            var metersPerUnit = mapRef.current.getView().getProjection().getMetersPerUnit();
            var geometryC = new _geom.Circle([editParam[0], editParam[1]], editParam[2] / metersPerUnit, 'XY');
            setCallbackData(editParam);
            featureC.setGeometry(geometryC);
          }
          if (drawParameter) {
            featureC.setStyle(new _Circle["default"](drawParameter));
          }
          setFeature(featureC);
          break;
        case 'LineString':
          var featureD = new _ol.Feature();
          var geometryD = new _geom.LineString(editParam);
          setCallbackData(editParam);
          featureD.setGeometry(geometryD);
          if (drawParameter) {
            featureD.setStyle(new _Polyline["default"](drawParameter));
          }
          setFeature(featureD);
          break;
      }
    }
  }, [refresh]);
  (0, _react.useEffect)(function () {
    if (!modalVisible) {
      setFeature(null);
      setPoi({});
    }
  }, [modalVisible]);
  (0, _react.useEffect)(function () {
    if (feature) {
      mapRef.current.fitView(feature);
    }
  }, [feature]);
  var onCancel = function onCancel() {
    closeModal && closeModal();
  };
  var submit = function submit() {
    if (graphicType === 'Marker') {
      (0, _search.regeoSearch)({
        location: callbackData,
        extensions: 'all'
      }).then(function (res) {
        callback && callback(callbackData, res);
      });
    } else {
      callback && callback(callbackData);
    }
  };
  var redraw = function redraw() {
    setFeature(null);
    setDrawType('');
    setTimeout(function () {
      setDrawType(graphicType);
    }, 100);
  };
  var onListClick = function onListClick(data) {
    if (graphicType === 'Marker') {
      var _data$geometryInfo$ln, _data$geometryInfo$ln2;
      var _feature = new _ol.Feature();
      var geometry = new _geom.Point((_data$geometryInfo$ln = data.geometryInfo.lngLats) === null || _data$geometryInfo$ln === void 0 ? void 0 : _data$geometryInfo$ln.split(','));
      setCallbackData((_data$geometryInfo$ln2 = data.geometryInfo.lngLats) === null || _data$geometryInfo$ln2 === void 0 ? void 0 : _data$geometryInfo$ln2.split(','));
      _feature.setGeometry(geometry);
      _feature.setStyle(new _Marker["default"]({
        image: _location["default"],
        scale: 1
      }));
      setFeature(_feature);
    } else {
      var selectLayer = mapRef.current.getLayerByKey('SELECT_LOCATION');
      selectLayer.loadData(data);
    }
  };
  var createdMap = function createdMap(map) {
    if ((limitAreaLngLat === null || limitAreaLngLat === void 0 ? void 0 : limitAreaLngLat.length) > 0 && !limitAreaFeature) {
      var _feature2 = new _ol.Feature({
        geometry: new _geom.Polygon(limitAreaLngLat)
      });
      source.addFeature(_feature2);
      setConstrainGeom(_feature2.getGeometry());
      if (!constrainGeom) {
        setConstrainGeom(_feature2.getGeometry());
      }
    } else if (limitAreaFeature) {
      setConstrainGeom(limitAreaFeature === null || limitAreaFeature === void 0 ? void 0 : limitAreaFeature.getGeometry());
      source.addFeature(limitAreaFeature);
    }
    map.addLayer(vector);
    var layer = new _FeatureLayer["default"]({
      key: 'LOCATION',
      style: {
        mode: 'Simple',
        render: {
          type: 'Marker',
          style: {
            image: _noLocation["default"],
            scale: 1
          }
        }
      },
      labelField: 'name'
    });
    map.addLayer(layer);
    var selectLayer = new _FeatureLayer["default"]({
      key: 'SELECT_LOCATION',
      style: {
        mode: 'Simple',
        render: {
          type: 'Marker',
          style: {
            image: _location["default"],
            scale: 1
          }
        }
      }
    });
    map.addLayer(selectLayer);
    mapRef.current = map;
    setRefresh(Math.random());
  };
  var pairArray = function pairArray(arr) {
    return arr.reduce(function (acc, curr, index) {
      if (index % 2 === 0) {
        acc.push([curr]);
      } else {
        acc[acc.length - 1].push(curr);
      }
      return acc;
    }, []);
  };
  var onDrawEnd = function onDrawEnd(feature, distance) {
    var _feature$getGeometry$, _feature$getGeometry$2, _feature$getGeometry$3;
    console.log(feature, feature.getGeometry(), distance);
    switch (graphicType) {
      case 'Marker':
        setCallbackData(feature.getGeometry().flatCoordinates);
        callbackDrawEnd && callbackDrawEnd(feature.getGeometry().flatCoordinate);
        break;
      case 'DragBox':
        var dataB = (_feature$getGeometry$ = feature.getGeometry().flatCoordinates) === null || _feature$getGeometry$ === void 0 ? void 0 : _feature$getGeometry$.slice(0, -2);
        setCallbackData([pairArray(dataB)]);
        callbackDrawEnd && callbackDrawEnd([pairArray(dataB)]);
        break;
      case 'Polygon':
        var dataC = (_feature$getGeometry$2 = feature.getGeometry().flatCoordinates) === null || _feature$getGeometry$2 === void 0 ? void 0 : _feature$getGeometry$2.slice(0, -2);
        setCallbackData([pairArray(dataC)]);
        callbackDrawEnd && callbackDrawEnd([pairArray(dataC)]);
        break;
      case 'Circle':
        var dataD = (_feature$getGeometry$3 = feature.getGeometry().flatCoordinates) === null || _feature$getGeometry$3 === void 0 ? void 0 : _feature$getGeometry$3.slice(0, -2);
        var metersPerUnit = mapRef.current.getView().getProjection().getMetersPerUnit();
        var radius = feature.getGeometry().getRadius() * metersPerUnit;
        dataD.push(radius);
        setCallbackData(dataD);
        callbackDrawEnd && callbackDrawEnd(dataD);
        break;
      case 'LineString':
        var dataE = feature.getGeometry().flatCoordinates;
        setCallbackData(pairArray(dataE));
        callbackDrawEnd && callbackDrawEnd(pairArray(dataE));
        break;
    }
  };
  var closeSearch = function closeSearch() {
    setCallbackData([]);
    setPoi({});
    setClearFlag(Math.random());
    var selectLayer = mapRef.current.getLayerByKey('SELECT_LOCATION');
    var noSelectLayer = mapRef.current.getLayerByKey('LOCATION');
    selectLayer.loadData({});
    noSelectLayer.loadData({});
    redraw();
  };
  var checkPoint = function checkPoint() {
    var center = mapRef.current.getView().getCenter();
    var feature = new _ol.Feature();
    var geometry = new _geom.Point(center);
    setCallbackData(center);
    feature.setGeometry(geometry);
    feature.setStyle(new _Marker["default"]({
      image: _location["default"],
      scale: 1
    }));
    setFeature(feature);
  };
  var renderCreated = function renderCreated() {
    if (mapRef.current) {
      return onCreated(mapRef.current);
    }
  };
  return modalVisible ? /*#__PURE__*/_react["default"].createElement(_VtxModal["default"], {
    title: graphicType === 'point' ? '定位' : '绘制',
    style: {
      top: 50
    },
    visible: modalVisible,
    maximize: false,
    isNotMoving: true,
    wrapClassName: styles.searchModal,
    bodyStyle: {
      height: "".concat(window.innerHeight * 0.7, "px")
    },
    maskClosable: false,
    onCancel: onCancel,
    footer: /*#__PURE__*/_react["default"].createElement("div", {
      className: styles.bottom
    }, /*#__PURE__*/_react["default"].createElement(_button["default"], {
      type: "primary",
      icon: /*#__PURE__*/_react["default"].createElement(_icons.CheckOutlined, null),
      onClick: submit
    }, "\u786E\u5B9A"), /*#__PURE__*/_react["default"].createElement(_button["default"], {
      icon: /*#__PURE__*/_react["default"].createElement(_icons.CloseOutlined, null),
      onClick: onCancel
    }, "\u5173\u95ED"))
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: styles.searchMap
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: styles.top
  }, /*#__PURE__*/_react["default"].createElement("div", {
    style: {
      width: '350px'
    }
  }, /*#__PURE__*/_react["default"].createElement(_Search["default"], {
    isChina: isChina,
    onConfirm: setPoi,
    clearFlag: clearFlag
  })), /*#__PURE__*/_react["default"].createElement(_button["default"], {
    key: "3",
    icon: /*#__PURE__*/_react["default"].createElement(_icons.CloseOutlined, null),
    onClick: closeSearch
  }, "\u6E05\u7A7A"), graphicType === 'Marker' ? /*#__PURE__*/_react["default"].createElement(_button["default"], {
    icon: /*#__PURE__*/_react["default"].createElement(_icons.EnvironmentOutlined, null),
    onClick: checkPoint
  }, "\u6821\u6B63") : /*#__PURE__*/_react["default"].createElement(_button["default"], {
    icon: /*#__PURE__*/_react["default"].createElement(_icons.EditOutlined, null),
    onClick: redraw
  }, "\u91CD\u65B0\u7ED8\u5236")), /*#__PURE__*/_react["default"].createElement("div", {
    className: styles.content
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: styles.content_right
  }, /*#__PURE__*/_react["default"].createElement(_map["default"], {
    height: {
      height: "".concat(window.innerHeight * 0.7, "px")
    },
    center: mapCenter,
    zoom: 10,
    tk: "1adbb6e47668cb33757cd799548bf1f4",
    createdMap: createdMap
  }, /*#__PURE__*/_react["default"].createElement(_featureDraw["default"], {
    feature: feature,
    drawType: drawType,
    onDrawEnd: onDrawEnd,
    markerStyle: {
      image: _location["default"],
      scale: 1
    },
    constrainGeom: constrainGeom
  }), (poi === null || poi === void 0 ? void 0 : poi.address) && /*#__PURE__*/_react["default"].createElement(_PoiList["default"], {
    poi: poi,
    onClick: onListClick
  }), props.children), renderCreated())))) : null;
};
//# sourceMappingURL=index.js.map
