"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 _lodash = _interopRequireDefault(require("lodash"));
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); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _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(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
var _Cesium = Cesium,
  CustomDataSource = _Cesium.CustomDataSource,
  ImageryLayer = _Cesium.ImageryLayer;
;
// 获取所有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) {
  _inherits(Layers, _EventEmitter);
  var _super = _createSuper(Layers);
  function Layers(options) {
    var _this;
    _classCallCheck(this, Layers);
    _this = _super.call(this);
    _this.map = options.map;
    _this.layers = [];
    return _this;
  }

  // 图层改变的时候
  _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 CustomDataSource) {
        map.dataSources.add(layer);
      } else if (layer instanceof ImageryLayer) {
        if (index) {
          map.scene.imageryLayers.add(layer, index);
        } else {
          map.scene.imageryLayers.add(layer);
        }
      }

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

    // 图层移除
  }, {
    key: "removeLayer",
    value: function removeLayer(layer) {
      var map = this.map;
      if (layer instanceof CustomDataSource) {
        map.dataSources.remove(layer);
      } else if (layer instanceof ImageryLayer) {
        map.scene.imageryLayers.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();
    }
  }, {
    key: "getAllKeys",
    value:
    // 获取 layers 里面的 key 集合
    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];
      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 = _lodash["default"].cloneDeep(layer.group);
            group.map(function (v) {
              v.show = _keys.includes(v.key);
            });
            if (!_lodash["default"].isEqual(group, layer.group)) {
              layer.setGroup(group);
            }
          } else {
            var show = keys.includes(a);
            if (show !== layer.show) {
              layer.setVisible(show);
            }
          }
        }
      });
    }

    // 根据 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);
        }
      });
    }
  }]);
  return Layers;
}(_EventEmitter2["default"]);
var _default = Layers;
exports["default"] = _default;
//# sourceMappingURL=Layers.js.map
