import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import { traverse } from './traverseUtils';
export var _HTREE_SPLIT_ = '__HTREE_SPLIT__'; // 拖拽时，被拖拽元素横向移动大于20px时，作为目标节点子节点

export var __AsTargetChildXOffset__ = 20; // 和第一个节点互换位置

export var __AsTargetSblingYOffset__ = 20;
export var __DEFAULT_FIELDNAMES__ = {
  title: 'title',
  key: 'key',
  children: 'children'
};
export function getNodeIsLeaf(treeNode, loadData) {
  var isLeaf = treeNode.isLeaf;
  if (typeof isLeaf === 'boolean') return isLeaf;

  if (treeNode.children) {
    return false;
  }

  return true;
}
export function calcEndCheckedKeys(checkedKeys) {
  return new Set(checkedKeys.map(function (keyArr) {
    if (keyArr && keyArr.length) {
      return keyArr.join(_HTREE_SPLIT_);
    }

    return '';
  }).filter(Boolean));
}
export function getExpandedKeys(treeData, expandedKeys, expandAll, autoExpandParent) {
  traverse(treeData, function (node) {
    var _node$children;

    // 全部默认展开
    if (expandAll && ((_node$children = node.children) === null || _node$children === void 0 ? void 0 : _node$children.length) && !expandedKeys.includes(node.nodeKey)) {
      expandedKeys.push(node.nodeKey);
      return;
    }

    var nParent = node.parent;

    while (!expandAll && autoExpandParent && nParent && expandedKeys.includes(node.nodeKey)) {
      expandedKeys.push(nParent.nodeKey);
      nParent = nParent.parent;
    }
  });
  return _toConsumableArray(expandedKeys);
}
export function getNumberKey(numberKeysSet, key) {
  var isNumber = numberKeysSet.has(key);
  return isNumber ? parseInt(key, 10) : key;
}
export function getTrulyKey(key, numberKeysSet) {
  if (!numberKeysSet) return [[key], key];
  if (typeof key !== 'string') return [[key], key];
  var path = key.split(_HTREE_SPLIT_);
  var endPath = path.map(function (p) {
    return getNumberKey(numberKeysSet, p);
  });
  return [endPath, endPath[endPath.length - 1]];
} // 根据选中的key值输出选中的节点

export function getCheckedNodes(treeNode, checkedKeys, fieldNames, keyToTreeNodeMap, numberKeysSet) {
  var checkedNodes = Array.from(checkedKeys).map(function (key) {
    return keyToTreeNodeMap.get(key);
  }).filter(Boolean);
  return handleExportNodes(checkedNodes, fieldNames, treeNode, numberKeysSet);
} // 处理输出的checkedNodes或者selectedNodes

export function handleExportNodes(target, fieldNames, targetNode, numberKeysSet) {
  var result = [];

  function getNewTreeNode(node) {
    var _getTrulyKey, _obj;

    var obj = (_obj = {
      level: node.level
    }, _defineProperty(_obj, fieldNames.title, node.title), _defineProperty(_obj, fieldNames.key, (_getTrulyKey = getTrulyKey(node.nodeKey, numberKeysSet)) === null || _getTrulyKey === void 0 ? void 0 : _getTrulyKey[1]), _defineProperty(_obj, fieldNames.children, node.children), _obj);
    helper(obj, node);
    return obj;
  }

  function helper(newNode, node) {
    if (!node.children) return;
    newNode.children = node.children.map(function (item) {
      var newItem = getNewTreeNode(item);

      if (item.children) {
        helper(newItem, item);
      }

      return newItem;
    });
  }

  target === null || target === void 0 ? void 0 : target.forEach(function (node) {
    var newNode = getNewTreeNode(node);
    result.push(newNode);
  });
  return [result, targetNode && getNewTreeNode(targetNode)];
}