"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getNodeIsLeaf = getNodeIsLeaf;
exports.calcEndCheckedKeys = calcEndCheckedKeys;
exports.getExpandedKeys = getExpandedKeys;
exports.getNumberKey = getNumberKey;
exports.getTrulyKey = getTrulyKey;
exports.getCheckedNodes = getCheckedNodes;
exports.handleExportNodes = handleExportNodes;
exports.__DEFAULT_FIELDNAMES__ = exports.__AsTargetSblingYOffset__ = exports.__AsTargetChildXOffset__ = exports._HTREE_SPLIT_ = void 0;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

var _traverseUtils = require("./traverseUtils");

var _HTREE_SPLIT_ = '__HTREE_SPLIT__'; // 拖拽时，被拖拽元素横向移动大于20px时，作为目标节点子节点

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

exports.__AsTargetChildXOffset__ = __AsTargetChildXOffset__;
var __AsTargetSblingYOffset__ = 20;
exports.__AsTargetSblingYOffset__ = __AsTargetSblingYOffset__;
var __DEFAULT_FIELDNAMES__ = {
  title: 'title',
  key: 'key',
  children: 'children'
};
exports.__DEFAULT_FIELDNAMES__ = __DEFAULT_FIELDNAMES__;

function getNodeIsLeaf(treeNode, loadData) {
  var isLeaf = treeNode.isLeaf;
  if (typeof isLeaf === 'boolean') return isLeaf;

  if (treeNode.children) {
    return false;
  }

  return true;
}

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

    return '';
  }).filter(Boolean));
}

function getExpandedKeys(treeData, expandedKeys, expandAll, autoExpandParent) {
  (0, _traverseUtils.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 (0, _toConsumableArray2.default)(expandedKeys);
}

function getNumberKey(numberKeysSet, key) {
  var isNumber = numberKeysSet.has(key);
  return isNumber ? parseInt(key, 10) : key;
}

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值输出选中的节点


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


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

  function getNewTreeNode(node) {
    var _getTrulyKey, _obj;

    var obj = (_obj = {
      level: node.level
    }, (0, _defineProperty2.default)(_obj, fieldNames.title, node.title), (0, _defineProperty2.default)(_obj, fieldNames.key, (_getTrulyKey = getTrulyKey(node.nodeKey, numberKeysSet)) === null || _getTrulyKey === void 0 ? void 0 : _getTrulyKey[1]), (0, _defineProperty2.default)(_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)];
}