"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(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
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(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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
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 _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 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
