"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["default"] = void 0;
var _Cluster2 = _interopRequireDefault(require("ol/source/Cluster"));
var _extent = require("ol/extent.js");
var _util = require("ol/util.js");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
var MyCluster = /*#__PURE__*/function (_Cluster) {
  _inherits(MyCluster, _Cluster);
  var _super = _createSuper(MyCluster);
  function MyCluster(options) {
    var _this;
    _classCallCheck(this, MyCluster);
    _this = _super.call(this, options);

    // 不加入聚合
    _this.unClusterFunc = options.unClusterFunc;

    // 需要提供类型字段，将会根据类型字段的值，自动进行分类聚合
    _this.field = options.field;

    // 根据分组聚合
    _this.group = options.group;

    // 聚合改变时触发的方法
    _this.clusterChange = options.clusterChange;
    return _this;
  }

  /**
   * 分组聚合
   */
  _createClass(MyCluster, [{
    key: "groupCluster",
    value: function groupCluster(feature, clustered, extent, mapDistance) {
      var _this2 = this;
      var geometry = this.geometryFunction(feature);
      if (geometry) {
        var coordinates = geometry.getCoordinates();
        (0, _extent.createOrUpdateFromCoordinate)(coordinates, extent);
        (0, _extent.buffer)(extent, mapDistance, extent);
        var neighbors = this.source.getFeaturesInExtent(extent);

        // 存在两个以上的点聚合
        if (neighbors.length > 1) {
          var _this$group;
          // 这次聚合按照当前中心点feature这一种类型进行
          var properties = feature.get('properties') || feature.getProperties();
          if (this.field) {
            neighbors = neighbors.filter(function (neighbor) {
              var pointProperties = neighbor.get('properties') || neighbor.getProperties();
              return pointProperties[_this2.field] === properties[_this2.field];
            });
          } else if ((_this$group = this.group) !== null && _this$group !== void 0 && _this$group.length) {
            neighbors = neighbors.filter(function (neighbor) {
              var pointProperties = neighbor.get('properties') || neighbor.getProperties();
              var i = _this2.group.findIndex(function (v) {
                var _v$filter;
                return v === null || v === void 0 ? void 0 : (_v$filter = v.filter) === null || _v$filter === void 0 ? void 0 : _v$filter.call(v, properties);
              });
              var a = _this2.group.findIndex(function (v) {
                var _v$filter2;
                return v === null || v === void 0 ? void 0 : (_v$filter2 = v.filter) === null || _v$filter2 === void 0 ? void 0 : _v$filter2.call(v, pointProperties);
              });
              return i !== -1 && a !== -1 && i === a;
            });
          }
        }

        // 先删除不需要聚合的点
        if (this.unClusterFunc) {
          var unCluster = neighbors.filter(function (neighbor) {
            return _this2.unClusterFunc(neighbor);
          });
          // 因为不需要聚合，所以这些点就直接单个展示，记录下这些要素
          unCluster.forEach(function (neighbor) {
            var uid = (0, _util.getUid)(neighbor);
            if (!(uid in clustered)) {
              clustered[uid] = true;
            }
            _this2.features.push(_this2.createCluster([neighbor], extent));
          });
          // 筛选除去不需要聚合的点后的几个点
          neighbors = neighbors.filter(function (neighbor) {
            return !_this2.unClusterFunc(neighbor);
          });
        }

        // 需要聚合的点
        neighbors = neighbors.filter(function (neighbor) {
          var uid = (0, _util.getUid)(neighbor);
          if (!(uid in clustered)) {
            clustered[uid] = true;
            return true;
          } else {
            return false;
          }
        });
        // 经过上面的操作后，可能会出现没有要聚合的点了，所以这里加一下判断
        if (neighbors.length > 0) {
          this.features.push(this.createCluster(neighbors, extent));
        }
      }
    }

    /**
     * 覆盖了原生的聚合方法
     * @returns
     */
  }, {
    key: "cluster",
    value: function cluster() {
      var _this$clusterChange;
      if (this.resolution === undefined || !this.source) {
        return;
      }
      var extent = (0, _extent.createEmpty)();
      var mapDistance = this.distance * this.resolution;
      var features = this.source.getFeatures();
      /**
       * @type {!Object<string, boolean>}
       */
      var clustered = {};
      for (var i = 0, ii = features.length; i < ii; i++) {
        var feature = features[i];
        if (!((0, _util.getUid)(feature) in clustered)) {
          this.groupCluster(feature, clustered, extent, mapDistance);
        }
      }
      (_this$clusterChange = this.clusterChange) === null || _this$clusterChange === void 0 ? void 0 : _this$clusterChange.call(this, this.features);
    }
  }]);
  return MyCluster;
}(_Cluster2["default"]);
var _default = MyCluster;
exports["default"] = _default;
//# sourceMappingURL=MyCluster.js.map
