"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.treeFilter = exports.getKeys = exports.getItemNum = exports.getItemByKey = exports.getIndeterminateAndChecked = exports["default"] = void 0;
var _LayerService = _interopRequireDefault(require("./LayerService"));
var _lodash = require("lodash");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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 _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
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); }
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; }
// 获取勾选状态
var getIndeterminateAndChecked = exports.getIndeterminateAndChecked = function getIndeterminateAndChecked() {
  var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var checkedKeys = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  var key = arguments.length > 2 ? arguments[2] : undefined;
  // 勾选数量
  var checkNum = checkedKeys.length;

  // 所有子项数量
  var allSubTotal = _getItemNum(data);
  var checked;
  var indeterminate;
  if (key === 'ALL') {
    checked = checkNum && checkNum >= allSubTotal; // 勾选数量等于所有子项数量
    indeterminate = checkNum && checkNum < allSubTotal; // 存在勾选但没有全选上
  } else {
    var subCheckNum = _treeFilter(data, function (v) {
      return checkedKeys.includes(v.key);
    }).length; // 子列表勾选数量
    checked = subCheckNum >= allSubTotal; // 勾选数量等于子项列表数量
    indeterminate = subCheckNum && subCheckNum < allSubTotal; // 存在勾选但没有全选上
  }
  return {
    indeterminate: indeterminate,
    checked: checked
  };
};

// 获取子项 key, 不含带 children 的那一项
var _getKeys = exports.getKeys = function getKeys(data) {
  var keys = [];
  data.map(function (v) {
    var _v$children;
    if ((_v$children = v.children) !== null && _v$children !== void 0 && _v$children.length) {
      keys.push.apply(keys, _toConsumableArray(_getKeys(v.children)));
    } else {
      keys.push(v.key);
    }
  });
  return keys;
};

// 计算子列表勾选数量
var _treeFilter = exports.treeFilter = function treeFilter(data, func) {
  if (!(data !== null && data !== void 0 && data.length)) {
    return [];
  }
  if (!(0, _lodash.isFunction)(func)) {
    return [];
  }
  var result = [];
  data.map(function (item) {
    var _item$children;
    if ((_item$children = item.children) !== null && _item$children !== void 0 && _item$children.length) {
      result.push.apply(result, _toConsumableArray(_treeFilter(item.children, func)));
    } else if (func(item)) {
      result.push(item);
    }
  });
  return result;
};

// 获取子项数量，不含带 children 的那一项
var _getItemNum = exports.getItemNum = function getItemNum(data) {
  var num = 0;
  data.map(function (v) {
    var _v$children2;
    if ((_v$children2 = v.children) !== null && _v$children2 !== void 0 && _v$children2.length) {
      num = num + _getItemNum(v.children);
    } else {
      num++;
    }
  });
  return num;
};
var _getItemByKey = exports.getItemByKey = function getItemByKey(data, key) {
  var item = data.find(function (v) {
    return v.key === key;
  });
  data.map(function (v) {
    var _v$children3;
    if ((_v$children3 = v.children) !== null && _v$children3 !== void 0 && _v$children3.length) {
      var _item = _getItemByKey(v.children, key);
      if (_item) {
        item = _item;
      }
    }
  });
  return item;
};
var LayersCheckService = exports["default"] = /*#__PURE__*/_createClass(function LayersCheckService(options) {
  var _this = this;
  _classCallCheck(this, LayersCheckService);
  // 创建图层
  _defineProperty(this, "createLayer", function (val) {
    var key = val.key,
      renderKey = val.renderKey;
    if (_LayerService["default"]["".concat(renderKey || key)]) {
      _LayerService["default"]["".concat(renderKey || key)](val, function (layer) {
        var _loopData = function loopData(data) {
          data.map(function (v) {
            if (v.key === key) {
              v.layer = layer;
            }
            if (v.children && v.children.length) {
              _loopData(v.children);
            }
          });
        };
        _loopData(_this.treeData);
      });
    }
  });
  _defineProperty(this, "setLayersVisible", function () {
    var showKeys = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
    // 展示图层
    _this.setLayersShow(showKeys);

    // 隐藏图层
    var keys = _getKeys(_this.treeData);
    var hideKeys = keys.filter(function (key) {
      return !showKeys.includes(key);
    });
    _this.setLayersHide(hideKeys);
  });
  // 展示图层
  _defineProperty(this, "setLayersShow", function (keys) {
    var layers = _this.map.getLayers();
    if (keys !== null && keys !== void 0 && keys.length) {
      keys.map(function (key) {
        var layer;
        layers.forEach(function (v) {
          if (v.key === key) {
            layer = v;
          }
        });
        if (layer) {
          layer.setVisible(true);
        } else {
          var val = _getItemByKey(_this.treeData, key);
          if (!(val !== null && val !== void 0 && val.customLoadLayer)) {
            _this.createLayer(_objectSpread({
              map: _this.map
            }, val));
          }
        }
      });
    }
  });
  // 隐藏图层
  _defineProperty(this, "setLayersHide", function (keys) {
    var layers = _this.map.getLayers();
    if (keys !== null && keys !== void 0 && keys.length) {
      layers.forEach(function (v) {
        if (keys.includes(v.key)) {
          v.setVisible(false);
        }
      });
    }
  });
  this.map = options.map;
  this.treeData = options.treeData;
});
//# sourceMappingURL=LayersCheckService.js.map
