"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.treeFilter = exports.getKeys = exports.getItemNum = exports.getIndeterminateAndChecked = void 0;
var _lodash = require("lodash");
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;
};
//# sourceMappingURL=LayersCheckService.js.map
