"use strict";

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