"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
exports.treeFind = treeFind;
exports.treeForEach = treeForEach;
exports.treeMap = treeMap;
exports.treeReduce = treeReduce;
exports.treeSome = treeSome;

function treeReduce(nodes, fn, initialValue) {
  var childName = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'children';
  var parentNode = arguments.length > 4 ? arguments[4] : undefined;
  return nodes.reduce(function (previousValue, node, index) {
    var newValue = fn(previousValue, node, index, parentNode);
    var children = node[childName];

    if (children && children.length) {
      return treeReduce(children, fn, newValue, childName, node);
    }

    return newValue;
  }, initialValue);
}

function treeMap(nodes, fn, sortFn) {
  var childName = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'children';
  var parentNode = arguments.length > 4 ? arguments[4] : undefined;
  var newNodes = nodes.map(function (node, index) {
    var newNode = fn(node, index, parentNode);
    var children = node[childName];

    if (children && children.length) {
      newNode[childName] = treeMap(children, fn, sortFn, childName, node);
    }

    return newNode;
  });

  if (sortFn) {
    newNodes.sort(sortFn);
  }

  return newNodes;
}

function treeForEach(nodes, fn) {
  var childName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
  var parentNode = arguments.length > 3 ? arguments[3] : undefined;
  nodes.forEach(function (node, index) {
    fn(node, index, parentNode);
    var children = node[childName];

    if (children && children.length) {
      treeForEach(children, fn, childName, node);
    }
  });
}

function treeSome(nodes, fn) {
  var childName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
  var parentNode = arguments.length > 3 ? arguments[3] : undefined;
  return nodes.some(function (node, index) {
    if (!fn(node, index, parentNode)) {
      var children = node[childName];

      if (children && children.length) {
        return treeSome(children, fn, childName, node);
      }
    }

    return true;
  });
}

function treeFind(nodes, fn) {
  var childName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
  var parentNode = arguments.length > 3 ? arguments[3] : undefined;
  var result;
  nodes.some(function (node, index) {
    var found = fn(node, index, parentNode);

    if (found) {
      result = node;
      return true;
    }

    var children = node[childName];

    if (children && children.length) {
      var foundChild = treeFind(children, fn, childName, node);

      if (foundChild) {
        result = foundChild;
        return true;
      }
    }

    return false;
  });
  return result;
}

var _default = {
  treeReduce: treeReduce,
  treeForEach: treeForEach,
  treeFind: treeFind,
  treeSome: treeSome,
  treeMap: treeMap
};
exports["default"] = _default;
//# sourceMappingURL=index.js.map
