"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.calcRangeKeys = calcRangeKeys;
exports.convertDirectoryKeysToNodes = convertDirectoryKeysToNodes;

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

function traverseNodesKey(treeData, callback) {
  function processNode(dataNode) {
    var key = dataNode.key,
        children = dataNode.children;

    if (callback(key, dataNode) !== false) {
      traverseNodesKey(children || [], callback);
    }
  }

  treeData.forEach(processNode);
}
/** 计算选中范围，只考虑expanded情况以优化性能 */


function calcRangeKeys(treeData, expandedKeys, startKey, endKey) {
  var keys = [];
  var record = 0
  /* None */
  ;

  if (startKey && startKey === endKey) {
    return [startKey];
  }

  if (!startKey || !endKey) {
    return [];
  }

  function matchKey(key) {
    return key === startKey || key === endKey;
  }

  traverseNodesKey(treeData, function (key) {
    if (record === 2
    /* End */
    ) {
      return false;
    }

    if (matchKey(key)) {
      // Match test
      keys.push(key);

      if (record === 0
      /* None */
      ) {
        record = 1
        /* Start */
        ;
      } else if (record === 1
      /* Start */
      ) {
        record = 2
        /* End */
        ;
        return false;
      }
    } else if (record === 1
    /* Start */
    ) {
      // Append selection
      keys.push(key);
    }

    if (expandedKeys.indexOf(key) === -1) {
      return false;
    }

    return true;
  });
  return keys;
}

function convertDirectoryKeysToNodes(treeData, keys) {
  var restKeys = (0, _toConsumableArray2["default"])(keys);
  var nodes = [];
  traverseNodesKey(treeData, function (key, node) {
    var index = restKeys.indexOf(key);

    if (index !== -1) {
      nodes.push(node);
      restKeys.splice(index, 1);
    }

    return !!restKeys.length;
  });
  return nodes;
}
//# sourceMappingURL=dictUtil.js.map
