"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 _CockpitIcon = _interopRequireDefault(require("@vtx/map-icon/lib/map-icon/CockpitIcon"));
var _FeatureLayer2 = _interopRequireDefault(require("../feature-layer/FeatureLayer"));
var _createFeatureCollection = require("../feature-layer/createFeatureCollection");
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 _get() { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get.bind(); } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(arguments.length < 3 ? target : receiver); } return desc.value; }; } return _get.apply(this, arguments); }
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 set(target, property, value, receiver) { if (typeof Reflect !== "undefined" && Reflect.set) { set = Reflect.set; } else { set = function set(target, property, value, receiver) { var base = _superPropBase(target, property); var desc; if (base) { desc = Object.getOwnPropertyDescriptor(base, property); if (desc.set) { desc.set.call(receiver, value); return true; } else if (!desc.writable) { return false; } } desc = Object.getOwnPropertyDescriptor(receiver, property); if (desc) { if (!desc.writable) { return false; } desc.value = value; Object.defineProperty(receiver, property, desc); } else { _defineProperty(receiver, property, value); } return true; }; } return set(target, property, value, receiver); }
function _set(target, property, value, receiver, isStrict) { var s = set(target, property, value, receiver || target); if (!s && isStrict) { throw new TypeError('failed to set property'); } return value; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
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 _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
var _Cesium = Cesium,
  CustomDataSource = _Cesium.CustomDataSource,
  defined = _Cesium.defined,
  VerticalOrigin = _Cesium.VerticalOrigin,
  HeightReference = _Cesium.HeightReference,
  HorizontalOrigin = _Cesium.HorizontalOrigin;
;
var DemandLayer = /*#__PURE__*/function (_FeatureLayer) {
  _inherits(DemandLayer, _FeatureLayer);
  var _super = _createSuper(DemandLayer);
  function DemandLayer(opt_options) {
    var _thisSuper, _this2;
    _classCallCheck(this, DemandLayer);
    var options = opt_options || {};
    var url = options.url,
      params = options.params,
      data = options.data,
      key = options.key,
      name = options.name,
      labelField = options.labelField,
      template = options.template,
      autoRefresh = options.autoRefresh,
      method = options.method,
      renderMode = options.renderMode,
      style = options.style,
      _options$show = options.show,
      show = _options$show === void 0 ? true : _options$show,
      legend = options.legend,
      group = options.group,
      map = options.map;
    if (group !== null && group !== void 0 && group.length) {
      if (!show) {
        group.map(function (f) {
          f.show = false;
        });
      } else {
        group.map(function (f) {
          var _f$show;
          f.show = (_f$show = f.show) !== null && _f$show !== void 0 ? _f$show : true;
        });
      }
    }
    _this2 = _super.call(this, options);
    _set((_thisSuper = _assertThisInitialized(_this2), _getPrototypeOf(DemandLayer.prototype)), "show", show, _thisSuper, true);

    // 接口地址
    _this2.url = url;

    // 接口地址请求类型
    _this2.method = method;

    // 接口查询参数
    _this2.params = params;

    // 图层标识
    _this2.key = key;

    // 图层名称
    _this2.name = name;

    // 渲染
    _this2.style = style;

    // 标签字段
    _this2.labelField = labelField;

    // 自动刷新标识
    _this2.autoRefresh = autoRefresh;

    // 模版内容
    _this2.template = template;

    // 渲染模式
    _this2.renderMode = renderMode || 'marker';

    // 图例
    _this2.legend = legend;

    // 过滤
    _this2.group = group;

    // 地图
    _this2.map = map;

    // 是否存在地址
    if (url) {
      // 执行
      _this2.loadUrlData(url, params, method);
    }

    // 直接加载数据
    if (data) {
      // 执行
      _this2.loadData(data);
    }
    return _this2;
  }

  // 重写父类的属性访问器
  _createClass(DemandLayer, [{
    key: "show",
    get: function get() {
      return _get(_getPrototypeOf(DemandLayer.prototype), "show", this);
    },
    set: function set(value) {
      this.setVisible(value);
    }

    // 获取地图
  }, {
    key: "getMap",
    value: function getMap() {
      return this.map;
    }

    // 根据条件获取图形数据
  }, {
    key: "getFeatureByFieldValue",
    value: function getFeatureByFieldValue(field, value) {
      return this.getSource().getFeatures().find(function (v) {
        return v.values_[field] === value;
      });
    }

    // 获取当前图层的所有图形
  }, {
    key: "getFeatures",
    value: function getFeatures() {
      return this.entities.values;
    }

    // 设置图层显示隐藏
  }, {
    key: "setVisible",
    value: function setVisible(visible) {
      _set(_getPrototypeOf(DemandLayer.prototype), "show", visible, this, true); // 将属性传递给父类

      var map = this.getMap && this.getMap();
      if (map) {
        map.layers.emit('visible', this, visible);
      }
    }

    // 设置过滤条件
  }, {
    key: "setGroup",
    value: function setGroup(group) {
      var features = this.getFeatures();
      var map = this.getMap && this.getMap();
      if (group !== null && group !== void 0 && group.length && features !== null && features !== void 0 && features.length) {
        features.map(function (feature) {
          var show = false;
          group.map(function (v) {
            if (!v.hasOwnProperty('show') || v.show) {
              var properties = map.getProperties(feature);
              if (v.filter(properties)) {
                show = true;
              }
            }
          });
          feature.show = show;
        });
        this.group = group;
        this.setVisibleByGroup();
      }
    }

    // 根据过滤条件组显示图层
  }, {
    key: "setVisibleByGroup",
    value: function setVisibleByGroup() {
      var group = this.group;
      if (group !== null && group !== void 0 && group.length) {
        var visible = false;
        group.map(function (v) {
          if (!v.hasOwnProperty('show') || v.show) {
            visible = true;
          }
        });
        this.setVisible(visible);
      }
    }

    // 设置渲染模式
  }, {
    key: "setRenderMode",
    value: function setRenderMode(renderMode) {
      this.hideClustering();

      // 展示聚合时设置样式为 marker 并且开启聚合监听
      if (renderMode === 'clustering') {
        this.renderMode = 'marker';
        this.setStyle();
        this.showClustering();
      } else {
        this.renderMode = renderMode;
        this.setStyle();
      }
    }

    // 自动刷新
  }, {
    key: "refresh",
    value: function refresh() {
      (0, _createFeatureCollection.getDataByHttp)(this.url, this.params, this.method).then(function (res) {
        var _res$features;
        if (res !== null && res !== void 0 && (_res$features = res.features) !== null && _res$features !== void 0 && _res$features.length) {}
      });
    }

    // 加载数据
  }, {
    key: "loadData",
    value: function loadData(data) {
      this.entities.removeAll();
      var geoJsonData = (0, _createFeatureCollection.createFeatureCollection)(Array.isArray(data) ? data : [data]);
      this.render(geoJsonData);
    }

    // 加载接口数据
  }, {
    key: "loadUrlData",
    value: function loadUrlData(url, params, method) {
      var _this3 = this;
      this.entities.removeAll();
      (0, _createFeatureCollection.getDataByHttp)(url, params, method).then(function (res) {
        var _res$features2;
        if (res !== null && res !== void 0 && (_res$features2 = res.features) !== null && _res$features2 !== void 0 && _res$features2.length) {
          _this3.render(res);
        }
      });
    }

    // 加载图形数据
  }, {
    key: "loadFeatures",
    value: function loadFeatures(features) {
      this.entities.removeAll();
      this.getSource().addFeatures(features);
    }

    // 渲染
  }, {
    key: "render",
    value: function render(geoJsonData) {
      var _this4 = this;
      if (geoJsonData) {
        var group = this.group;
        var features = geoJsonData.features;
        features.map(function (v) {
          var entity = createFeature(v);
          if (entity) {
            var _this4$group, _style, _style2;
            var show = true;
            var style = _this4.style;

            // 针对过滤时的处理
            if ((_this4$group = _this4.group) !== null && _this4$group !== void 0 && _this4$group.length) {
              show = false;
              group.map(function (f) {
                if (f.filter(v.properties)) {
                  style = f.style;
                  if (!f.hasOwnProperty('show') || f.show) {
                    show = true;
                  }
                }
              });
            }
            setRenderer(entity, style);
            if (((_style = style) === null || _style === void 0 ? void 0 : _style.type) === 'Marker' && (_style2 = style) !== null && _style2 !== void 0 && _style2.style) {
              var _style$style = style.style,
                image = _style$style.image,
                imageType = _style$style.imageType,
                imageKey = _style$style.imageKey;
              if (image && imageType) {
                if (imageType === 'gif') {
                  entity.setGifImage(image, imageKey);
                }
              }
            }
            entity.show = show;
            entity.type = _this4.modalType || _this4.key;
            entity.key = _this4.key;
            entity.properties = v.properties || {};
            _this4.entities.add(entity);
          }
        });
      }
    }

    // 设置样式
  }, {
    key: "setStyle",
    value: function setStyle() {
      var _this5 = this;
      if (this.renderMode === 'point') {
        this.entities.values.map(function (entity) {
          setRenderer(entity, {
            type: 'Marker',
            style: {
              type: 'PointIcon',
              color: '#0080ff',
              size: 15
            }
          });
        });
      } else {
        this.entities.values.map(function (entity) {
          setRenderer(entity, _this5.style);
        });
      }
    }

    // 展示聚合
  }, {
    key: "showClustering",
    value: function showClustering() {
      // 是否聚合
      this.customStyle();
      this.clustering.pixelRange = 100;
      this.clustering.minimumClusterSize = 2;
      this.clustering.enabled = true;
    }

    // 隐藏聚合
  }, {
    key: "hideClustering",
    value: function hideClustering() {
      this.clustering.enabled = false;
      if (defined(this.removeListener) && this.removeListener) {
        this.removeListener();
        this.removeListener = undefined;
      }
    }

    // 自定义样式
  }, {
    key: "customStyle",
    value: function customStyle() {
      var _this = this;
      if (defined(this.removeListener) && this.removeListener) {
        this.removeListener();
        this.removeListener = undefined;
      } else {
        this.removeListener = this.clustering.clusterEvent.addEventListener(function (clusteredEntities, cluster) {
          cluster.label.show = false;
          cluster.billboard.show = true;
          cluster.billboard.id = cluster.label.id;
          cluster.billboard.verticalOrigin = VerticalOrigin.BOTTOM;
          cluster.billboard.heightReference = HeightReference.CLAMP_TO_GROUND;
          cluster.billboard.horizontalOrigin = HorizontalOrigin.CENTER;
          cluster.billboard.disableDepthTestDistance = 10000;
          var text = clusteredEntities.length;
          var color = 'rgb(9,220,107)';
          if (clusteredEntities.length >= 50) {
            text = '50+';
            color = 'rgb(220,33,9)';
          } else if (clusteredEntities.length >= 40) {
            text = '40+';
            color = 'rgb(232,119,5)';
          } else if (clusteredEntities.length >= 30) {
            text = '30+';
            color = 'rgb(232,194,4)';
          } else if (clusteredEntities.length >= 20) {
            text = '20+';
            color = 'rgba(20, 232, 4, 1)';
          } else if (clusteredEntities.length >= 10) {
            text = '10+';
            color = 'rgb(0,128,255)';
          }
          cluster.billboard.image = new _CockpitIcon["default"]({
            canvasIcon: _this.style,
            color: color,
            hideFooter: true,
            info: [{
              text: text,
              fontSize: 24
            }]
          }).getImgUrl();
        });
      }
    }
  }]);
  return DemandLayer;
}(_FeatureLayer2["default"]);
var _default = DemandLayer;
exports["default"] = _default;
//# sourceMappingURL=DemandLayer.js.map
