import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import { handleExportNodes } from './_utils';
import { traverse } from './traverseUtils';
/**
 * 计算当前应该选中的节点
 * @param treeNode 当前节点
 * @param checked 当前节点的选中状态
 * @param checkedKeysSet 选中的节点key集合
 */

export function getCheckedKeysSet(treeNode, checked, checkedKeysSet) {
  if (checked) {
    checkedKeysSet.add(treeNode.nodeKey);
    var parentNode = treeNode.parent;

    while (parentNode && !checkedKeysSet.has(parentNode.nodeKey)) {
      var _parentNode$children;

      // 父节点的子节点是否全部选中
      if ((_parentNode$children = parentNode.children) === null || _parentNode$children === void 0 ? void 0 : _parentNode$children.every(function (child) {
        return checkedKeysSet.has(child.nodeKey);
      })) {
        checkedKeysSet.add(parentNode.nodeKey);
      }

      parentNode = parentNode.parent;
    } // 改变子节点状态


    traverse(treeNode.children, function (currentNode) {
      checkedKeysSet.add(currentNode.nodeKey);
    });
  } else {
    checkedKeysSet.delete(treeNode.nodeKey); // 父节点状态也需要改变

    var _parentNode = treeNode.parent;

    while (_parentNode && checkedKeysSet.has(_parentNode.nodeKey)) {
      checkedKeysSet.delete(_parentNode.nodeKey);
      _parentNode = _parentNode.parent;
    } // 改变子节点状态


    traverse(treeNode.children, function (currentNode) {
      checkedKeysSet.delete(currentNode.nodeKey);
    });
  }
}
/**
 * @param checkedKeys 选中的节点key值集合
 * @param keyToTreeNodeMap key->treeNode的映射
 * @returns 半选的节点key值
 */

function getHalfCheckedKeysSet(checkedKeysSet, keyToTreeNodeMap) {
  var halfKeysSet = new Set();
  checkedKeysSet.forEach(function (fKey) {
    var node = keyToTreeNodeMap.get(fKey);

    while (node && node.parent) {
      var _curParentNode$childr, _curParentNode$childr2, _curParentNode$childr3;

      var curParentNode = node.parent; // 父节点的子节点任意一个选中或者子节点任意一个为半选状态

      if (!((_curParentNode$childr = curParentNode.children) === null || _curParentNode$childr === void 0 ? void 0 : _curParentNode$childr.every(function (child) {
        return checkedKeysSet.has(child.nodeKey);
      })) && ((_curParentNode$childr2 = curParentNode.children) === null || _curParentNode$childr2 === void 0 ? void 0 : _curParentNode$childr2.some(function (child) {
        return checkedKeysSet.has(child.nodeKey);
      })) || ((_curParentNode$childr3 = curParentNode.children) === null || _curParentNode$childr3 === void 0 ? void 0 : _curParentNode$childr3.some(function (child) {
        return halfKeysSet.has(child.nodeKey);
      }))) {
        halfKeysSet.add(curParentNode.nodeKey);
      }

      node = curParentNode;
    }
  });
  return halfKeysSet;
}
/**
 * 根据传入的checkedKeys计算出所有应该半选和全选的key
 * 传入的checkedKeys不一定就是全部选中的节点，可能只是部分
 * @param checkedKeys
 */


export function getNodeKeys(checkedKeys, keyToTreeNodeMap) {
  var endCheckedKeys = new Set(_toConsumableArray(checkedKeys));
  checkedKeys.forEach(function (key) {
    var node = keyToTreeNodeMap.get(key);

    if (node) {
      getCheckedKeysSet(node, true, endCheckedKeys);
    }
  });
  var halfKeys = getHalfCheckedKeysSet(endCheckedKeys, keyToTreeNodeMap);
  return [endCheckedKeys, halfKeys];
}
export function getExportCheckedKeys(checkedKeysSet, keyToTreeNodeMap) {
  var filterChildrenCheckedKeys = new Set();
  var filterAncestorCheckedKeys = new Set();
  var allCheckedKeysArr = Array.from(checkedKeysSet);
  checkedKeysSet.forEach(function (checkedKey) {
    var tNode = keyToTreeNodeMap.get(checkedKey); // 检查父节点是否选中

    var treeNodeParent = tNode === null || tNode === void 0 ? void 0 : tNode.parent;

    while (treeNodeParent) {
      // 如果父节点选中，则子节点不再作为结果输出
      // 即只输出最大公共祖先节点
      if (checkedKeysSet.has(treeNodeParent.nodeKey)) {
        filterChildrenCheckedKeys.add(checkedKey);
        filterAncestorCheckedKeys.add(treeNodeParent.nodeKey);
      }

      treeNodeParent = treeNodeParent.parent;
    }
  }); // 保证只返回选中根节点，比如0-1，0-1-0，0-1-1都选中，则只返回0-1

  var exportCheckedKeys = allCheckedKeysArr.filter(function (key) {
    return !filterChildrenCheckedKeys.has(key);
  });
  var exportFilterAnCheckedKeys = allCheckedKeysArr.filter(function (key) {
    return !filterAncestorCheckedKeys.has(key);
  });
  return [exportCheckedKeys, exportFilterAnCheckedKeys];
}
/**
 *
 * @param treeNode 当前选中的节点
 * @param checked 是否选中
 * @param checkedKeysSet 选中节点key集合
 * @param keyToTreeNodeMap key -> node映射
 * @returns []
 */

export function onTreeNodeCheck(treeNode, checked, checkedKeysSet, keyToTreeNodeMap) {
  getCheckedKeysSet(treeNode, checked, checkedKeysSet);
  var halfKeysSet = getHalfCheckedKeysSet(checkedKeysSet, keyToTreeNodeMap);

  var _getExportCheckedKeys = getExportCheckedKeys(checkedKeysSet, keyToTreeNodeMap),
      _getExportCheckedKeys2 = _slicedToArray(_getExportCheckedKeys, 2),
      exportCheckedKeys = _getExportCheckedKeys2[0],
      exportFilterAnCheckedKeys = _getExportCheckedKeys2[1];

  return [exportCheckedKeys, exportFilterAnCheckedKeys, halfKeysSet];
}
export function onTreeNodeSelect(treeNode, selectedKeysSet, fieldNames, keyToTreeNodeMap, multiple, numberKeysSet) {
  var selected = treeNode.selected,
      nodeKey = treeNode.nodeKey;

  if (!selected) {
    if (!multiple) {
      selectedKeysSet.clear();
    }

    selectedKeysSet.add(nodeKey);
  } else {
    selectedKeysSet.delete(nodeKey);
  }

  var temp = _toConsumableArray(selectedKeysSet);

  var selectedNodes = temp.map(function (key) {
    return keyToTreeNodeMap.get(key);
  });
  return [temp].concat(_toConsumableArray(handleExportNodes(selectedNodes, fieldNames, treeNode, numberKeysSet)));
}