"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 _GroupLayer = _interopRequireDefault(require("../group-layer/GroupLayer"));
var _ImageLayer = _interopRequireDefault(require("../image-layer/ImageLayer"));
var _ImageTileLayer = _interopRequireDefault(require("../image-layer/ImageTileLayer"));
var _FeatureLayer = _interopRequireDefault(require("../feature-layer/FeatureLayer"));
var _WmsLayer = _interopRequireDefault(require("../wms-layer/WmsLayer"));
var _lodash = require("lodash");
var _FlowLineLayer = _interopRequireDefault(require("../flow-line-layer/FlowLineLayer"));
var _ArrowFlowLayer = _interopRequireDefault(require("../arrow-flow-layer/ArrowFlowLayer"));
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 getLayersVisibleKeys = function getLayersVisibleKeys(data, map) {
  var visible = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
  var keys = [];
  function loop(data, map, visible) {
    data.map(function (v) {
      var _v$layers;
      if (v.key) {
        var layer = map.layers.getLayerByKey(v.key);
        if (layer && !(layer instanceof _GroupLayer["default"])) {
          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);
          }

          // 如果group的显示的长度和layer的不一致 则不显示
          // if (
          //     layer.show === visible &&
          //     layer.group?.filter(v => v.show)?.length == layer.group?.length
          // ) {
          //     keys.push(v.key);
          // }
        }
      }
      if ((_v$layers = v.layers) !== null && _v$layers !== void 0 && _v$layers.length) {
        loop(v.layers, map, visible);
      }
    });
  }
  loop(data, map, visible);
  return keys;
};
// 获取所有keys
var _getAllKeys = function getAllKeys(data) {
  var keys = [];
  data.map(function (v) {
    var _v$layers2;
    if ((_v$layers2 = v.layers) !== null && _v$layers2 !== void 0 && _v$layers2.length) {
      keys.push.apply(keys, _toConsumableArray(_getAllKeys(v.layers)));
    }
    keys.push(v.key);
  });
  return keys;
};

// 获取所有keys(包含group)
var _getAllLayersKeys = function getAllLayersKeys(data) {
  var keys = [];
  data.map(function (v) {
    var _v$layers3, _v$group;
    if ((_v$layers3 = v.layers) !== null && _v$layers3 !== void 0 && _v$layers3.length) {
      keys.push.apply(keys, _toConsumableArray(_getAllLayersKeys(v.layers)));
    }
    keys.push(v.key);
    if (v !== null && v !== void 0 && (_v$group = v.group) !== null && _v$group !== void 0 && _v$group.length) {
      v.group.map(function (f) {
        if (f.key) {
          keys.push(f.key);
        }
      });
    }
  });
  return keys;
};

// 获取图层
var getLayers = function getLayers(data, layer, _keys, visible) {
  var layers = [];
  // 判断查询 全部的 or 隐藏的 or 显示的
  function loop(data, layer, _keys, visible) {
    data.map(function (v) {
      var _v$layers4;
      var isVisible = typeof visible !== 'boolean' ? true : v.displayName !== 'GroupLayer' ? (v === null || v === void 0 ? void 0 : v.getVisible()) === visible : false;
      if (v.key && v instanceof layer && isVisible) {
        if (_keys) {
          var keys = Array.isArray(_keys) ? _keys : [_keys];
          if (keys.includes(v.key)) {
            layers.push(v);
          }
        } else {
          layers.push(v);
        }
      }
      if ((_v$layers4 = v.layers) !== null && _v$layers4 !== void 0 && _v$layers4.length) {
        var newLayer = loop(v.layers, layer, _keys, visible);
        if (newLayer !== null && newLayer !== void 0 && newLayer.length) {
          layers.push(newLayer);
        }
      }
    });
  }
  loop(data, layer, _keys, visible);
  return layers;
};
var Layers = exports["default"] = /*#__PURE__*/function (_EventEmitter) {
  function Layers(options) {
    var _this;
    _classCallCheck(this, Layers);
    _this = _callSuper(this, Layers);
    _this.map = options.map;
    _this.layers = [];
    return _this;
  }
  // 图层改变的时候
  _inherits(Layers, _EventEmitter);
  return _createClass(Layers, [{
    key: "onChange",
    value: function onChange() {
      this.emit('change', this.layers);
    }
  }, {
    key: "addLayer",
    value: function addLayer(layer) {
      var map = this.map;
      if (layer instanceof _FeatureLayer["default"] || layer instanceof _ImageTileLayer["default"] || layer instanceof _ImageLayer["default"] || layer instanceof _FlowLineLayer["default"] || layer instanceof _ArrowFlowLayer["default"]) {
        map.addLayer(layer instanceof _ArrowFlowLayer["default"] ? layer.vectorLayer : layer);
      }
      if (layer.name && layer.layerControl) {
        this.layers.push(layer);
        this.emit('add', layer);
        this.onChange();
      }
    }
  }, {
    key: "removeLayer",
    value: function removeLayer(layer) {
      var map = this.map;
      if (layer instanceof _FeatureLayer["default"] || layer instanceof _ImageTileLayer["default"] || layer instanceof _ImageLayer["default"] || layer instanceof _WmsLayer["default"] || layer instanceof _FlowLineLayer["default"] || layer instanceof _ArrowFlowLayer["default"]) {
        map.removeLayer(layer instanceof _ArrowFlowLayer["default"] ? layer.vectorLayer : layer);
      }
      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 _this$layers;
      var map = this.map;
      (_this$layers = this.layers) === null || _this$layers === void 0 || _this$layers.map(function (layer) {
        if (layer instanceof _FeatureLayer["default"] || layer instanceof _ImageTileLayer["default"] || layer instanceof _ImageLayer["default"] || layer instanceof _FlowLineLayer["default"] || layer instanceof _ArrowFlowLayer["default"] || layer instanceof _WmsLayer["default"]) {
          map.removeLayer(layer instanceof _ArrowFlowLayer["default"] ? layer.vectorLayer : layer);
        }
      });
      this.layers = [];
      this.emit('clear');
      this.onChange();
    }
  }, {
    key: "getLayerByKey",
    value: function getLayerByKey(_keys) {
      var _this$getAllLayers;
      return (_this$getAllLayers = this.getAllLayers()) === null || _this$getAllLayers === void 0 ? void 0 : _this$getAllLayers.find(function (layer) {
        return _keys && layer.key && layer.key === _keys;
      });
    }
  }, {
    key: "getAllLayers",
    value: function getAllLayers() {
      var layers = [];
      function loop(data) {
        data.map(function (item) {
          var _item$layers;
          layers.push(item);
          if (item !== null && item !== void 0 && (_item$layers = item.layers) !== null && _item$layers !== void 0 && _item$layers.length) {
            loop(item.layers);
          }
        });
      }
      loop(this.layers);
      return layers;
    }
    // 获取 layers 里面的 key 集合
  }, {
    key: "getAllKeys",
    value: function getAllKeys() {
      return _getAllKeys(this.layers);
    }

    // 获取 layers 里面的 key 集合
  }, {
    key: "getAllLayersKeys",
    value: function getAllLayersKeys() {
      return _getAllLayersKeys(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, visible) {
      var imageLayer = getLayers(this.layers, _ImageLayer["default"], _keys, visible);
      var imageTileLayer = getLayers(this.layers, _ImageTileLayer["default"], _keys, visible);
      return [].concat(_toConsumableArray(imageLayer), _toConsumableArray(imageTileLayer));
    }

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

    // 根据 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.layers.getLayerByKey(a);
        if (layer && !(layer instanceof _GroupLayer["default"])) {
          var _layer$group2;
          if ((_layer$group2 = layer.group) !== null && _layer$group2 !== void 0 && _layer$group2.length) {
            var group = (0, _lodash.cloneDeep)(layer.group);
            var show = false;
            group.forEach(function (v) {
              v.show = _keys.includes(v.key);
              if (_keys.includes(v.key)) {
                show = true;
              }
            });
            if (JSON.stringify(group) !== JSON.stringify(layer.group)) {
              layer.setGroup(group);
            }
            layer.show = show;
            layer.setVisible(show);
          } else {
            var _show = keys.includes(a);
            if (_show !== layer.show) {
              layer.show = _show;
              layer.setVisible(_show);
            }
          }
        }
      });
      map.layers.onChange();
    }

    // 根据 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.layers.getLayerByKey(a);
        if (layer) {
          var _layer$group3;
          if ((_layer$group3 = layer.group) !== null && _layer$group3 !== void 0 && _layer$group3.length) {
            var group = (0, _lodash.cloneDeep)(layer.group);
            group.forEach(function (v) {
              v.show = !_keys.includes(v.key);
            });
            if (!(0, _lodash.isEqual)(group, layer.group)) {
              layer.setGroup(group);
            }
          } else {
            var show = !keys.includes(a);
            if (show !== layer.show) {
              layer.show = show;
              layer.setVisible(show);
            }
          }
        }
      });
      map.layers.onChange();
    }
  }]);
}(_EventEmitter2["default"]);
//# sourceMappingURL=Layers.js.map
