"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var turf = _interopRequireWildcard(require("@turf/turf"));
var _utils = require("../utils");
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 _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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, 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); }
/**
 * @description:
 * @param {*} map
 * @param {*} feature
 * @param {*} layers 图层
 * @param {*} highLayerFn 高亮方法
 * @return {*}
 */
var getWfsData = function getWfsData(_ref) {
  var _feature$getGeometry;
  var map = _ref.map,
    feature = _ref.feature,
    layers = _ref.layers,
    highLayerFn = _ref.highLayerFn;
  var features = [],
    data = [],
    poly = null;
  var area = feature === null || feature === void 0 || (_feature$getGeometry = feature.getGeometry()) === null || _feature$getGeometry === void 0 ? void 0 : _feature$getGeometry.getCoordinates();
  if (area) {
    poly = turf.polygon(area);
  }
  var geometry = feature === null || feature === void 0 ? void 0 : feature.getGeometry();
  var extent = geometry === null || geometry === void 0 ? void 0 : geometry.getExtent();
  var getFeatures = function getFeatures(feature, layer) {
    feature.key = layer.key;
    var listKeys = feature.getKeys();
    var _data = {};
    listKeys.forEach(function (item) {
      if (feature.get(item)) {
        _data[item] = feature.get(item);
        _data.feature = feature;
        _data.label = layer.name;
        _data.codeField = layer.codeField;
        _data.key = layer.key;
      }
    });
    data.push(_objectSpread({}, _data));
    features.push(feature);
  };
  var handelFeature = function handelFeature(_newFeature, _newLayer) {
    var _newFeature$getGeomet, _newFeature$getGeomet2, _newFeature$getGeomet3, _lngLat$;
    var lngLat = _newFeature === null || _newFeature === void 0 || (_newFeature$getGeomet = _newFeature.getGeometry()) === null || _newFeature$getGeomet === void 0 ? void 0 : _newFeature$getGeomet.getCoordinates();
    var pt;
    var intersects = false;
    if ((_newFeature === null || _newFeature === void 0 || (_newFeature$getGeomet2 = _newFeature.getGeometry()) === null || _newFeature$getGeomet2 === void 0 ? void 0 : _newFeature$getGeomet2.getType()) === 'Point') {
      lngLat.map(function (v) {
        return v = Number(v);
      });
      pt = turf.point(lngLat);
      // 判断在点在多边形内
      if (poly && pt) {
        intersects = turf.booleanPointInPolygon(pt, poly);
      }
      // 过滤出绘制图形中的数据
      if (intersects) {
        getFeatures(_newFeature, _newLayer);
      }
    }
    if ((_newFeature === null || _newFeature === void 0 || (_newFeature$getGeomet3 = _newFeature.getGeometry()) === null || _newFeature$getGeomet3 === void 0 ? void 0 : _newFeature$getGeomet3.getType()) === 'Polygon' && lngLat !== null && lngLat !== void 0 && (_lngLat$ = lngLat[0]) !== null && _lngLat$ !== void 0 && _lngLat$.length) {
      var _lngLat$2;
      lngLat === null || lngLat === void 0 || lngLat[0].map(function (v) {
        return v = Number(v);
      });
      lngLat === null || lngLat === void 0 || (_lngLat$2 = lngLat[0]) === null || _lngLat$2 === void 0 || _lngLat$2.map(function (item) {
        pt = turf.point(item);
        if (poly && pt && !intersects) {
          intersects = turf.booleanPointInPolygon(pt, poly);
        }
      });
      if (intersects) {
        getFeatures(_newFeature, _newLayer);
      }
    }
  };
  if (extent) {
    layers.map(function (layer) {
      var source = layer.getSource();
      if (source.forEachFeatureIntersectingExtent && extent) {
        source.forEachFeatureIntersectingExtent(extent, function (_feature) {
          var _feature$values_;
          if ((_feature === null || _feature === void 0 || (_feature$values_ = _feature.values_) === null || _feature$values_ === void 0 || (_feature$values_ = _feature$values_.features) === null || _feature$values_ === void 0 ? void 0 : _feature$values_.length) > 1) {
            _feature.values_.features.map(function (v) {
              handelFeature(v, layer);
            });
          } else {
            handelFeature(_feature, layer);
          }
        });
      }
    });
  } else {
    layers.map(function (layer) {
      var source = layer.getSource();
      if (source.forEachFeature) {
        source.forEachFeature(function (_feature) {
          var _feature$values_2;
          if ((_feature === null || _feature === void 0 || (_feature$values_2 = _feature.values_) === null || _feature$values_2 === void 0 || (_feature$values_2 = _feature$values_2.features) === null || _feature$values_2 === void 0 ? void 0 : _feature$values_2.length) > 1) {
            _feature.values_.features.map(function (v) {
              getFeatures(v, layer);
            });
          } else {
            getFeatures(_feature, layer);
          }
        });
      }
    });
  }
  // 高亮
  if (highLayerFn) {
    highLayerFn(features);
  } else {
    (0, _utils.setHighlightLayer)(map, features);
  }
  return {
    data: data,
    features: features
  };
};
var _default = exports["default"] = getWfsData;
//# sourceMappingURL=FeatureSelect.js.map
