"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
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");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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); }
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 _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, 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); }
// TODO
var GroupLayer = /*#__PURE__*/function () {
  function GroupLayer(opt_options) {
    var _this = this;
    _classCallCheck(this, GroupLayer);
    // 获取图层最后一个 index 值
    _defineProperty(this, "getLastIndex", function (_index) {
      var index = 0;
      var layers = _this.layers;
      if (layers !== null && layers !== void 0 && layers.length) {
        if (_index && layers.findIndex(function (v) {
          return _index < v.zIndex;
        }) !== -1) {
          index = layers.findIndex(function (v) {
            return _index < v.zIndex;
          });
        } else {
          index = layers.length - 1;
        }
      }
      return index;
    });
    var _ref = opt_options || {},
      map = _ref.map,
      key = _ref.key,
      name = _ref.name,
      legend = _ref.legend,
      _ref$layerControl = _ref.layerControl,
      layerControl = _ref$layerControl === void 0 ? true : _ref$layerControl,
      _ref$layers = _ref.layers,
      _layers = _ref$layers === void 0 ? [] : _ref$layers,
      _ref$show = _ref.show,
      show = _ref$show === void 0 ? true : _ref$show;
    this.map = map;
    this.key = key;
    this.name = name;
    this.legend = legend;
    this.layerControl = layerControl;
    this.layers = _layers;
    this.show = show;
    this.displayName = 'GroupLayer';
  }
  return _createClass(GroupLayer, [{
    key: "addLayer",
    value: function addLayer(layer, groupIndex, mapIndex) {
      if (this.hasLayer(layer)) {
        return;
      }
      if (layer instanceof _FeatureLayer["default"] || layer instanceof _ImageLayer["default"] || layer instanceof _ImageTileLayer["default"] || layer instanceof _WmsLayer["default"]) {
        this.map.addLayer(layer);
      }
      if ((0, _lodash.isNumber)(groupIndex)) {
        this.layers.splice(groupIndex, 0, layer);
      } else {
        this.layers.push(layer);
      }
      if (layer.layerControl) {
        this.map.layers.onChange();
      }
    }
  }, {
    key: "removeLayer",
    value: function removeLayer(layer) {
      if (!this.hasLayer(layer)) {
        return;
      }
      if (layer instanceof _FeatureLayer["default"] || layer instanceof _ImageLayer["default"] || layer instanceof _ImageTileLayer["default"] || layer instanceof _WmsLayer["default"]) {
        this.map.removeLayer(layer);
      }
      this.layers = this.layers.filter(function (v) {
        return v !== layer;
      });
      if (layer.layerControl) {
        this.map.layers.onChange();
      }
    }
  }, {
    key: "hasLayer",
    value: function hasLayer(layer) {
      return this.layers.findIndex(function (v) {
        return v === layer;
      }) !== -1;
    }
  }, {
    key: "getLayers",
    value: function getLayers() {
      return this.layers;
    }
  }, {
    key: "setLayers",
    value: function setLayers(layers) {
      this.layers = layers;
    }
  }, {
    key: "indexOf",
    value: function indexOf(layer) {
      return this.layers.findIndex(function (v) {
        return v === layer;
      });
    }
  }, {
    key: "setVisible",
    value: function setVisible(visible) {
      this.show = visible;
      this.layers.map(function (v) {
        v.show = visible;
      });
      this.map.layers.onChange();
    }
  }]);
}();
var _default = exports["default"] = GroupLayer;
//# sourceMappingURL=GroupLayer.js.map
