"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.filterTreeData = filterTreeData;
exports.findNodeWithPath = findNodeWithPath;
exports.findNodeWithoutPath = findNodeWithoutPath;
exports.getAllNodeKey = getAllNodeKey;
exports.getTreeLevel = getTreeLevel;
exports.getTreeLevelKey = getTreeLevelKey;
exports.getTreeNodeLevel = getTreeNodeLevel;
exports.getTreeNodeProps = getTreeNodeProps;
exports.loop = loop;
var _excluded = ["selectable", "checkable", "editable", "draggable", "disabled", "checkboxDisabled", "isLeaf", "treeNodeProps"];
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _objectDestructuringEmpty(obj) { if (obj == null) throw new TypeError("Cannot destructure " + obj); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
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 _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 _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
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 _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; }
/**
 * 深度遍历节点
 * @param data 数据
 * @param func 处理方法
 * @param aliasMap 标识
 * @param level 深度
 */
function loop(data, func, aliasMap) {
  var level = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
  var _ref = aliasMap !== null && aliasMap !== void 0 ? aliasMap : {},
    _ref$children = _ref.children,
    children = _ref$children === void 0 ? 'children' : _ref$children;
  if (data && data.length > 0) {
    data.forEach(function (item) {
      func(item, level, data);
      if (Array.isArray(item[children])) {
        loop(item[children], func, {
          children: children
        }, level + 1);
      }
    });
  }
}
/**
 * 深度遍历并查找节点（只保留二级路径）
 * @param data 数据
 * @param predicate 条件
 * @param aliasMap 标识
 * @returns
 */
function findNodeWithoutPath(data, predicate, aliasMap) {
  var _ref2 = aliasMap !== null && aliasMap !== void 0 ? aliasMap : {},
    _ref2$children = _ref2.children,
    children = _ref2$children === void 0 ? 'children' : _ref2$children;
  // 如果已经没有节点了，结束递归
  if (!(data && data.length)) {
    return [];
  }
  var newChildren = [];
  var _iterator = _createForOfIteratorHelper(data),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var node = _step.value;
      if (predicate(node)) {
        var _node$children;
        // 如果节点符合条件，直接加入新的节点集
        newChildren.push(node);
        node.children = findNodeWithoutPath((_node$children = node[children]) !== null && _node$children !== void 0 ? _node$children : [], predicate);
      } else {
        var _node$children2;
        // 如果当前节点不符合条件，递归过滤子节点，
        // 把符合条件的子节点提升上来，并入新节点集
        newChildren.push.apply(newChildren, _toConsumableArray(findNodeWithoutPath((_node$children2 = node[children]) !== null && _node$children2 !== void 0 ? _node$children2 : [], predicate)));
      }
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
  return newChildren;
}
/**
 * 深度遍历并查找节点（保留全部路径）
 * @param data 数据
 * @param predicate 条件
 * @param aliasMap 标识
 * @returns
 */
function findNodeWithPath(data, predicate, aliasMap) {
  var _ref3 = aliasMap !== null && aliasMap !== void 0 ? aliasMap : {},
    _ref3$children = _ref3.children,
    children = _ref3$children === void 0 ? 'children' : _ref3$children;
  var getNodes = function getNodes(result, node) {
    if (predicate(node)) {
      result.push(node);
      return result;
    }
    if (Array.isArray(node[children])) {
      var nodes = node[children].reduce(getNodes, []);
      if (nodes.length) {
        result.push(_objectSpread(_objectSpread({}, node), {}, {
          nodes: nodes
        }));
      }
    }
    return result;
  };
  return data.reduce(getNodes, []);
}
/**
 * 获取树所有节点 key
 * @param data 数据
 * @param aliasMap 标识
 * @returns
 */
function getAllNodeKey(data, aliasMap) {
  var _ref4 = aliasMap !== null && aliasMap !== void 0 ? aliasMap : {},
    _ref4$key = _ref4.key,
    key = _ref4$key === void 0 ? 'value' : _ref4$key,
    _ref4$children = _ref4.children,
    children = _ref4$children === void 0 ? 'children' : _ref4$children;
  var keyList = [];
  loop(data, function (node) {
    keyList.push(node[key]);
  }, {
    children: children
  });
  return keyList;
}
/**
 * 获取当前节点的层级
 * @param data 数据
 * @param value 需要获取层级的 key
 * @param aliasMap 标识
 * @returns
 */
function getTreeNodeLevel(data, value, aliasMap) {
  var _ref5 = aliasMap !== null && aliasMap !== void 0 ? aliasMap : {},
    _ref5$key = _ref5.key,
    key = _ref5$key === void 0 ? 'value' : _ref5$key,
    _ref5$children = _ref5.children,
    children = _ref5$children === void 0 ? 'children' : _ref5$children;
  var result = 0;
  loop(data, function (item, level) {
    if (item[key] === value) {
      result = level !== null && level !== void 0 ? level : 0;
    }
  }, {
    children: children
  });
  return result;
}
/**
 * 获取树的层级
 * @param data 数据
 * @param aliasMap 标识
 * @returns
 */
function getTreeLevel(data, aliasMap) {
  var _ref6 = aliasMap !== null && aliasMap !== void 0 ? aliasMap : {},
    _ref6$children = _ref6.children,
    children = _ref6$children === void 0 ? 'children' : _ref6$children;
  var result = 0;
  loop(data, function (item, level) {
    if (result < level) {
      result = level;
    }
  }, {
    children: children
  });
  return result + 1;
}
/**
 * 获取树层级对应的所有 key
 * @param data
 * @param level
 * @param aliasMap
 * @returns
 */
function getTreeLevelKey(data, level, aliasMap) {
  var _ref7 = aliasMap !== null && aliasMap !== void 0 ? aliasMap : {},
    _ref7$key = _ref7.key,
    key = _ref7$key === void 0 ? 'value' : _ref7$key,
    _ref7$children = _ref7.children,
    children = _ref7$children === void 0 ? 'children' : _ref7$children;
  var result = [];
  // TODO 此处可做性能优化
  loop(data, function (item, currentLevel) {
    if (currentLevel + 1 <= level) {
      result.push(item === null || item === void 0 ? void 0 : item[key]);
    }
  }, {
    children: children
  });
  return result;
}
/**
 * 深度遍历并查找节点（展开查找到的字节点路径）
 * @param nodes 数据
 * @param predicate 条件
 * @param aliasMap 标识
 * @returns
 */
function filterTreeData(nodes, predicate, aliasMap) {
  if (!(nodes && nodes.length)) {
    return {
      filterTreeValue: []
    };
  }
  var _ref8 = aliasMap !== null && aliasMap !== void 0 ? aliasMap : {},
    _ref8$children = _ref8.children,
    children = _ref8$children === void 0 ? 'children' : _ref8$children;
  var filterTreeValue = [];
  var _iterator2 = _createForOfIteratorHelper(nodes),
    _step2;
  try {
    for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
      var node = _step2.value;
      var others = _extends({}, (_objectDestructuringEmpty(node), node));
      if (predicate(node)) {
        var _node$children3;
        filterTreeValue.push(_objectSpread(_objectSpread({}, others), {}, {
          children: filterTreeData((_node$children3 = node[children]) !== null && _node$children3 !== void 0 ? _node$children3 : [], predicate, aliasMap).filterTreeValue
        }));
      } else {
        var subs = filterTreeData(node[children], predicate, aliasMap);
        if (subs && subs.filterTreeValue.length) {
          filterTreeValue.push(_objectSpread(_objectSpread({}, others), {}, {
            children: subs.filterTreeValue
          }));
        }
      }
    }
  } catch (err) {
    _iterator2.e(err);
  } finally {
    _iterator2.f();
  }
  return {
    filterTreeValue: filterTreeValue
  };
}
/**
 * 获取TreeNodeProps
 * @param item
 */
function getTreeNodeProps(item) {
  var selectable = item.selectable,
    checkable = item.checkable,
    editable = item.editable,
    draggable = item.draggable,
    disabled = item.disabled,
    checkboxDisabled = item.checkboxDisabled,
    isLeaf = item.isLeaf,
    _item$treeNodeProps = item.treeNodeProps,
    treeNodeProps = _item$treeNodeProps === void 0 ? {} : _item$treeNodeProps,
    others = _objectWithoutProperties(item, _excluded);
  var nodeProps = _objectSpread({
    selectable: selectable,
    checkable: checkable,
    editable: editable,
    draggable: draggable,
    disabled: disabled,
    checkboxDisabled: checkboxDisabled,
    isLeaf: isLeaf
  }, treeNodeProps);
  return {
    nodeProps: Object.fromEntries(Object.entries(nodeProps).filter(function (_ref9) {
      var _ref10 = _slicedToArray(_ref9, 2),
        _ = _ref10[0],
        v = _ref10[1];
      return v !== undefined;
    })),
    labelProps: others
  };
}