"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.traverse = traverse;
exports.findNodeInTreeData = findNodeInTreeData;
exports.traverseTreeNode = traverseTreeNode;

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

var _utils = require("./_utils");

function traverse(treeData, callback, afterCallback) {
  function dfs(data) {
    if (!data || !data.length) return;

    for (var i = 0; i < data.length; i += 1) {
      var node = data[i];
      callback === null || callback === void 0 ? void 0 : callback(node);
      dfs(node.children);
      afterCallback === null || afterCallback === void 0 ? void 0 : afterCallback(node);
    }
  }

  dfs(treeData);
}

function findNodeInTreeData(key, treeData) {
  var fieldName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'nodeKey';
  var target = null;
  traverse(treeData, function (node) {
    if (node[fieldName] === key) {
      target = node;
    }
  });
  return target;
}

function getNodeKey(horizontal, currentNode) {
  var path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
  var nodeKey = typeof currentNode.key !== 'undefined' && currentNode.key !== null ? currentNode.key : +new Date();

  if (horizontal) {
    // 横向树所有的key全部转换为string
    nodeKey = path + nodeKey;
  } // return nodeKey.toString();


  return nodeKey;
}

function traverseTreeNode(treeData, fieldNames, keyToTreeNodeMap, numberKeysSet, horizontal, // callback?: (node: TreeNode) => void,
titleToKeyMap) {
  if (!treeData || !treeData.length) return [];

  function dfs(data, prevNode, level, path) {
    return data.map(function (node) {
      if (node) {
        var tempNode = {};
        Object.keys(fieldNames).forEach(function (key) {
          tempNode[key] = node[fieldNames[key]];
        });
        tempNode = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, node), tempNode);
        var newNode = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, tempNode), {}, {
          level: level,
          isLeaf: tempNode.isLeaf,
          nodeKey: getNodeKey(Boolean(horizontal), tempNode, path),
          parent: prevNode
        }); // 保存每个节点的节点信息，方便节点查询，减少时间复杂度

        keyToTreeNodeMap.set(newNode.nodeKey, newNode); // 保存title到key的映射
        // ReactNode形式的title无法解析，除了string

        if (titleToKeyMap && typeof newNode.title === 'string') {
          titleToKeyMap.set(newNode.title, newNode.key);
        } // 如果输入的key为number类型，则存起来，保证输出的key也是number


        if (typeof newNode.key === 'number' && horizontal) {
          numberKeysSet.add(newNode.key.toString()); // numberKeysSet.add(newNode.key);
        } // callback?.(newNode);


        if (newNode.children) {
          newNode.children = dfs(newNode.children, newNode, level + 1, newNode.nodeKey + _utils._HTREE_SPLIT_);
        }

        return newNode;
      }

      return node;
    });
  }

  var result = dfs(treeData, null, 0);
  return result;
}