"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _style = require("ol/style");
var _config = _interopRequireDefault(require("../_mapUtil/config"));
var _request = _interopRequireDefault(require("../_mapUtil/request"));
var _utils = require("../_mapUtil/utils");
var turf = _interopRequireWildcard(require("@turf/turf"));
var _FeatureLayer = _interopRequireDefault(require("@vtx/ol-map/lib/feature-layer/FeatureLayer"));
var _source = require("ol/source");
var _PublicService = _interopRequireDefault(require("../_mapUtil/PublicService"));
var _format = require("ol/format");
var _analysisLayer = require("@vtx/ol-map/lib/analysis-layer");
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 _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var HydrantAnalysis = exports["default"] = /*#__PURE__*/function () {
  function HydrantAnalysis(props) {
    _classCallCheck(this, HydrantAnalysis);
    this.map = props.map;
    this.key = 'HydrantAnalysis'; // 相应的分析标识
    this.initFun();
  }
  return _createClass(HydrantAnalysis, [{
    key: "initFun",
    value: function initFun() {
      var _this = this;
      this.analysisLayer = new _FeatureLayer["default"]({
        source: new _source.Vector(),
        zIndex: 10,
        style: function style(feature) {
          return _this === null || _this === void 0 ? void 0 : _this.style(feature);
        }
      });
      this.map.addLayer(this.analysisLayer);
    }
  }, {
    key: "addFeatures",
    value: function addFeatures(features) {
      var _this$analysisLayer;
      (_this$analysisLayer = this.analysisLayer) === null || _this$analysisLayer === void 0 || (_this$analysisLayer = _this$analysisLayer.getSource()) === null || _this$analysisLayer === void 0 || _this$analysisLayer.addFeatures(features);
    }
  }, {
    key: "clearSource",
    value: function clearSource() {
      var _this$analysisLayer2;
      (_this$analysisLayer2 = this.analysisLayer) === null || _this$analysisLayer2 === void 0 || (_this$analysisLayer2 = _this$analysisLayer2.getSource()) === null || _this$analysisLayer2 === void 0 || _this$analysisLayer2.clear();
    }
    /**
     * @description: 多个多边形合成一个多边形
     * @param {*} features 框选内的feature
     * @param {*} feature  框选框的feature
     * @param {*} drawType 绘制类型
     * @return {*}
     */
  }, {
    key: "unionMultiPolygon",
    value: function unionMultiPolygon(features, feature, drawType) {
      var isCalculationIncludes = false;
      var area = 0;
      var polygons = [];
      if (features !== null && features !== void 0 && features.length) {
        features.map(function (feature) {
          var geometry = feature === null || feature === void 0 ? void 0 : feature.getGeometry();
          var coordinates = geometry === null || geometry === void 0 ? void 0 : geometry.getCoordinates();
          if (coordinates) {
            var Polygon = turf.polygon(coordinates);
            polygons.push(Polygon);
          }
        });
      }
      var _coordinates = (0, _utils.getCoordinates)(feature, drawType);
      if (polygons !== null && polygons !== void 0 && polygons.length && _coordinates) {
        // 单个多边形
        var union = polygons[0];
        // 多个多边形
        if ((polygons === null || polygons === void 0 ? void 0 : polygons.length) > 1) union = turf.union.apply(turf, polygons);
        var _union = turf.polygon(_coordinates);

        // 计算交集
        isCalculationIncludes = this.calculationIncludes(union, _union);
      }
      return {
        isCalculationIncludes: isCalculationIncludes,
        area: area
      };
    }
  }, {
    key: "calculationIncludes",
    value: function calculationIncludes(data, _data) {
      var _data$geometry, _data$geometry2;
      var isCalculationIncludes;
      if ((data === null || data === void 0 || (_data$geometry = data.geometry) === null || _data$geometry === void 0 ? void 0 : _data$geometry.type) !== 'MultiPolygon' && (_data === null || _data === void 0 || (_data$geometry2 = _data.geometry) === null || _data$geometry2 === void 0 ? void 0 : _data$geometry2.type) !== 'MultiPolygon') {
        isCalculationIncludes = turf.booleanContains(data, _data);
      }
      return isCalculationIncludes;
    }
    /**
     * @description: 判断多边形包含关系
     * @param {*} data
     * @param {*} _data
     * @return {*} true 包含
     */
  }, {
    key: "style",
    value: function style() {
      var style = [new _style.Style({
        fill: new _style.Fill({
          color: 'rgba(24,132,255, 0.1)'
        }),
        stroke: new _style.Stroke({
          color: '#1884FF',
          width: 2
        }),
        image: new _style.Circle({
          radius: 4,
          fill: new _style.Fill({
            color: 'rgba(0,155,255, 1)'
          })
        })
      })];
      return style;
    }
  }, {
    key: "getData",
    value: function getData(_ref) {
      var _feature$getGeometry,
        _this2 = this;
      var codeList = _ref.codeList,
        feature = _ref.feature,
        activeBtn = _ref.activeBtn,
        highLayer = _ref.highLayer,
        hydrantRadius = _ref.hydrantRadius,
        setTip = _ref.setTip,
        callback = _ref.callback;
      var params = {
        type: 2 // 1-供水管线 2-消防栓 3-阀门
      };
      var lngLat = feature === null || feature === void 0 || (_feature$getGeometry = feature.getGeometry()) === null || _feature$getGeometry === void 0 || (_feature$getGeometry = _feature$getGeometry.getCoordinates()) === null || _feature$getGeometry === void 0 ? void 0 : _feature$getGeometry.join(',');
      if (lngLat) {
        var geometryInfoDTO = {
          coordType: 'wgs84',
          lngLats: lngLat,
          type: 'polygon '
        };
        // 如果是点选
        if (activeBtn == 'Point') {
          var g = feature === null || feature === void 0 ? void 0 : feature.getGeometry().getInteriorPoint().getCoordinates();
          geometryInfoDTO = {
            coordType: 'wgs84',
            lngLats: "".concat(g[0], ",").concat(g[1]),
            type: 'point '
          };
        }
        params.geometryInfoDTO = geometryInfoDTO;
      }
      params.codeList = codeList;
      if (!codeList.length) {
        callback({});
        return;
      }
      var url = "".concat(_config["default"].publicUrlZNFX, "/api/gisAnalyze/hydrantAnalysis");
      _request["default"].post(url, {
        body: params
      }).then(function (res) {
        var _res$data;
        callback(res);
        if ((res === null || res === void 0 ? void 0 : res.result) === 0 && res !== null && res !== void 0 && (_res$data = res.data) !== null && _res$data !== void 0 && _res$data.length) {
          _this2.getFeatures({
            data: res === null || res === void 0 ? void 0 : res.data,
            highLayer: highLayer,
            hydrantRadius: hydrantRadius,
            feature: feature,
            activeBtn: activeBtn,
            setTip: setTip
          });
        }
      })["catch"](function () {
        callback({});
      });
    }
  }, {
    key: "destory",
    value: function destory() {
      this.clearSource();
      this.map.removeLayer(this.analysisLayer);
    }
  }, {
    key: "radiusAnalysis",
    value: function radiusAnalysis(_ref2) {
      var _this$analysisLayer3;
      var features = _ref2.features,
        hydrantRadius = _ref2.hydrantRadius,
        feature = _ref2.feature,
        activeBtn = _ref2.activeBtn,
        setTip = _ref2.setTip;
      this.clearSource();
      var bufferFeatures = [];
      var format = new _format.GeoJSON();
      (features === null || features === void 0 ? void 0 : features.length) && features.map(function (feature) {
        var buffered, bufferFeature;
        var coordinates = (0, _utils.getCoordinates)(feature);
        if (coordinates) {
          var point = turf.point(coordinates);
          buffered = turf.buffer(point, hydrantRadius / 1000, {
            units: 'kilometers'
          });
          bufferFeature = format.readFeature(buffered);
          bufferFeatures.push(bufferFeature);
        }
      });
      this === null || this === void 0 || this.addFeatures(bufferFeatures);
      var _this$unionMultiPolyg = this.unionMultiPolygon(bufferFeatures, feature, activeBtn),
        isCalculationIncludes = _this$unionMultiPolyg.isCalculationIncludes;
      var tip = isCalculationIncludes ? '分析范围内室外消防栓满足消防给水需求！' : "\u5BA4\u5916\u6D88\u9632\u6813\u672A\u6EE1\u8DB3\u6D88\u9632\u7ED9\u6C34\u9700\u6C42\uFF01\u5EFA\u8BAE\u65B0\u589E\u6D88\u9632\u6813\uFF01";
      if (activeBtn !== 'Point') {
        setTip(tip);
      }
      this.map.fitView((_this$analysisLayer3 = this.analysisLayer) === null || _this$analysisLayer3 === void 0 ? void 0 : _this$analysisLayer3.getSource());
    }
    // 服务半径
  }, {
    key: "getFeatures",
    value: function getFeatures(_ref3) {
      var _this3 = this;
      var data = _ref3.data,
        highLayer = _ref3.highLayer,
        hydrantRadius = _ref3.hydrantRadius,
        feature = _ref3.feature,
        activeBtn = _ref3.activeBtn,
        setTip = _ref3.setTip;
      var codes = _PublicService["default"].getFilterParams(data, 'code');
      var filter = codes ? "code in (".concat(codes, ")") : '';
      var layers = this.map.layers.getAllLayers().filter(function (_layer) {
        return _layer === null || _layer === void 0 ? void 0 : _layer.show;
      });
      (0, _analysisLayer.getWFSFeatureInfoByLayers)(layers, filter).then(function (features) {
        if (features !== null && features !== void 0 && features.length) {
          highLayer === null || highLayer === void 0 || highLayer.setHighlight(features);
          _this3.radiusAnalysis({
            features: features,
            hydrantRadius: hydrantRadius,
            feature: feature,
            activeBtn: activeBtn,
            setTip: setTip
          });
        }
      });
    }
  }]);
}();
//# sourceMappingURL=HydrantAnalysis.js.map
