"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 _EventEmitter2 = _interopRequireDefault(require("../_util/EventEmitter"));
var _ImageLayer = _interopRequireDefault(require("../image-layer/ImageLayer"));
var _FeatureLayer = _interopRequireDefault(require("../feature-layer/FeatureLayer"));
var _cesium = require("cesium");
var _lodash = require("lodash");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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); }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread 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 _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
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; }
// 获取所有keys
var _getAllKeys = function getAllKeys(data) {
  var keys = [];
  data.map(function (v) {
    var _v$layers;
    if ((_v$layers = v.layers) !== null && _v$layers !== void 0 && _v$layers.length) {
      keys.push.apply(keys, _toConsumableArray(_getAllKeys(v.layers)));
    } else {
      keys.push(v.key);
    }
  });
  return keys;
};

// 获取显示或隐藏的keys
var getLayersVisibleKeys = function getLayersVisibleKeys(data, map) {
  var visible = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
  var keys = [];
  data.map(function (v) {
    var _v$layers2;
    if (v.key) {
      var layer = map.getLayerByKey(v.key);
      if (layer) {
        var _layer$group;
        if ((_layer$group = layer.group) !== null && _layer$group !== void 0 && _layer$group.length) {
          if (visible) {
            layer.group.map(function (f) {
              if (!f.hasOwnProperty('show') || f.show) {
                keys.push(f.key);
              }
            });
          } else {
            layer.group.map(function (f) {
              if (!f.show) {
                keys.push(f.key);
              }
            });
          }
        } else if (layer.show === visible) {
          keys.push(v.key);
        }
      }
    }
    if ((_v$layers2 = v.layers) !== null && _v$layers2 !== void 0 && _v$layers2.length) {
      var _keys = getLayersVisibleKeys(v.layers, map, visible);
      if (_keys !== null && _keys !== void 0 && _keys.length) {
        keys.push.apply(keys, _toConsumableArray(_keys));
      }
    }
  });
  return keys;
};

// 获取图层
var getLayers = function getLayers(data, layer, _keys) {
  var layers = [];
  data.map(function (v) {
    var _v$layers3;
    if (v.key && v instanceof layer) {
      if (_keys) {
        var keys = Array.isArray(_keys) ? _keys : [_keys];
        if (keys.includes(v.key)) {
          layers.push(v.key);
        }
      } else {
        layers.push(v.key);
      }
    }
    if ((_v$layers3 = v.layers) !== null && _v$layers3 !== void 0 && _v$layers3.length) {
      var _keys2 = getLayers(v.layers, layer);
      if (_keys2 !== null && _keys2 !== void 0 && _keys2.length) {
        layers.push.apply(layers, _toConsumableArray(_keys2));
      }
    }
  });
  return layers;
};
var Layers = /*#__PURE__*/function (_EventEmitter) {
  function Layers(options) {
    var _this;
    _classCallCheck(this, Layers);
    _this = _callSuper(this, Layers);
    _this.map = options.map;
    _this.layers = [];
    _this.highlightFeature = null;
    return _this;
  }

  // 图层改变的时候
  _inherits(Layers, _EventEmitter);
  return _createClass(Layers, [{
    key: "onChange",
    value: function onChange() {
      this.emit('change', this.layers);
    }

    // 图层新增
  }, {
    key: "addLayer",
    value: function addLayer(layer, index) {
      var map = this.map;
      if (layer instanceof _cesium.CustomDataSource) {
        map.dataSources.add(layer);
      } else if (layer instanceof _cesium.ImageryLayer) {
        if (index) {
          map.scene.imageryLayers.add(layer, index);
        } else {
          map.scene.imageryLayers.add(layer);
        }
      } else if (layer instanceof _cesium.Primitive) {
        if (index) {
          map.scene.primitives.add(layer, index);
        } else {
          map.scene.primitives.add(layer);
        }
      }

      // 图层是否含有name字段，作为加入到图层树的判断依据
      if (layer.name && layer.layerControl) {
        if ((0, _lodash.isNumber)(index)) {
          this.layers.splice(index, 0, layer);
        } else {
          this.layers.push(layer);
        }
        this.onChange();
        this.emit('add', layer);
      }
    }

    // 图层移除
  }, {
    key: "removeLayer",
    value: function removeLayer(layer) {
      var map = this.map;
      if (layer instanceof _cesium.CustomDataSource) {
        map.dataSources.remove(layer);
      } else if (layer instanceof _cesium.ImageryLayer) {
        map.scene.imageryLayers.remove(layer);
      } else if (layer instanceof _cesium.Primitive) {
        map.scene.primitives.remove(layer);
      }

      // 图层是否含有name字段，作为加入到图层树的判断依据
      if (layer.name && layer.layerControl) {
        this.layers = this.layers.filter(function (v) {
          return v !== layer;
        });
        this.emit('remove', layer);
        this.onChange();
      }
    }

    // 图层清空
  }, {
    key: "clearLayers",
    value: function clearLayers() {
      var map = this.map;
      var imageryLayers = map.getImageryLayers() || [];
      map.dataSources.removeAll();
      imageryLayers.map(function (layer) {
        if (layer !== null && layer !== void 0 && layer.key) {
          map.imageryLayers.remove(layer);
        }
      });
      map.entities.removeAll();
      this.layers = [];
      this.emit('clear');
      this.onChange();
    }

    // 获取 layers 里面的 key 集合
  }, {
    key: "getAllKeys",
    value: function getAllKeys() {
      return _getAllKeys(this.layers);
    }

    // 获取当前展示的 keys
  }, {
    key: "getShowKeys",
    value: function getShowKeys() {
      return getLayersVisibleKeys(this.layers, this.map, true);
    }

    // 获取当前隐藏的 keys
  }, {
    key: "getHiddenKeys",
    value: function getHiddenKeys() {
      return getLayersVisibleKeys(this.layers, this.map, false);
    }

    // 获取 ImageLayer 图层
  }, {
    key: "getImageLayers",
    value: function getImageLayers(_keys) {
      return getLayers(this.layers, _ImageLayer["default"], _keys);
    }

    // 获取 FeatureLayer 图层
  }, {
    key: "getFeatureLayers",
    value: function getFeatureLayers(_keys) {
      return getLayers(this.layers, _FeatureLayer["default"], _keys);
    }

    // 根据key查找imageryLayers图层
  }, {
    key: "getImageryLayers",
    value: function getImageryLayers(_keys) {
      var _map$imageryLayers;
      var map = this.map;
      var layers = (map === null || map === void 0 || (_map$imageryLayers = map.imageryLayers) === null || _map$imageryLayers === void 0 ? void 0 : _map$imageryLayers._layers) || [];
      if (_keys && layers.length) {
        var keys = Array.isArray(_keys) ? _keys : [_keys];
        return layers.filter(function (layer) {
          return layer.key && keys.includes(layer.key);
        });
      }
      return layers;
    }

    // 根据 keys 设置图层的显示，其他的隐藏
  }, {
    key: "setShowByKeys",
    value: function setShowByKeys(_keys) {
      var map = this.map;
      var keys = Array.isArray(_keys) ? _keys : [_keys];
      var layerKey = null;
      if (this.highlightFeature) {
        var _map$getLayerByFeatur;
        layerKey = (_map$getLayerByFeatur = map.getLayerByFeature(this.highlightFeature)) === null || _map$getLayerByFeatur === void 0 ? void 0 : _map$getLayerByFeatur.key;
      }
      var highlightShow = false;
      this.getAllKeys().map(function (a) {
        var layer = map.getLayerByKey(a);
        if (layer) {
          var _layer$group2;
          if ((_layer$group2 = layer.group) !== null && _layer$group2 !== void 0 && _layer$group2.length) {
            var group = (0, _lodash.cloneDeep)(layer.group);
            group.map(function (v) {
              v.show = _keys.includes(v.key);
              if (v.show && layerKey === layer.key) {
                highlightShow = true;
              }
            });
            if (!(0, _lodash.isEqual)(group, layer.group)) {
              layer.setGroup(group);
            }
          } else {
            var show = keys.includes(a);
            if (show !== layer.show) {
              layer.setVisible(show);
              if (layerKey === layer.key) {
                highlightShow = true;
              }
            }
          }
        }
      });
      if (!highlightShow) {
        map.hideTemplate();
      }
    }

    // 根据 keys 设置图层的隐藏，其他的显示
  }, {
    key: "setHideByKeys",
    value: function setHideByKeys(_keys) {
      var map = this.map;
      var keys = Array.isArray(_keys) ? _keys : [_keys];
      this.getAllKeys().map(function (a) {
        var layer = map.getLayerByKey(a);
        if (layer) {
          layer.show = !keys.includes(a);
        }
      });
    }
  }, {
    key: "getLayerIndex",
    value: function getLayerIndex(key) {
      return this.layers.findIndex(function (layer) {
        return layer.key === key;
      });
    }
  }]);
}(_EventEmitter2["default"]);
var _default = exports["default"] = Layers;
//# sourceMappingURL=Layers.js.map
