"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 _FeatureLayer = _interopRequireDefault(require("../feature-layer/FeatureLayer"));
var _lodash = require("lodash");
var _FlowLineLayer = _interopRequireDefault(require("../flow-line-layer/FlowLineLayer"));
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(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(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() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
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 _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; }
// 获取显示或隐藏的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 _ImageLayer["default"] || layer instanceof _FlowLineLayer["default"]) {
        map.addLayer(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 _ImageLayer["default"] || layer instanceof _FlowLineLayer["default"]) {
        map.removeLayer(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 _ImageLayer["default"] || layer instanceof _FlowLineLayer["default"]) {
          map.removeLayer(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) {
      return getLayers(this.layers, _ImageLayer["default"], _keys, visible);
    }

    // 获取 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
