"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.createFilter = void 0;

var createFilter = function createFilter(treeSelector) {
  var sizeThreshold = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 3;
  return function (searchText) {
    var label = treeSelector.name;
    var children = treeSelector.children;
    var rootId = treeSelector.rootId();
    var snippets = searchText.toLowerCase().split(' ').filter(function (x) {
      return x.length >= sizeThreshold;
    });
    var filteredOut = {};
    var expanded = {};
    expanded[rootId] = true;
    if (snippets.length === 0) return [filteredOut, expanded];

    var predicate = function predicate(nodeId) {
      var txt = label(nodeId).toLowerCase();
      var isFound = snippets.find(function (snippet) {
        return txt.indexOf(snippet) >= 0;
      }) !== undefined;
      return isFound;
    };

    function filterNode(nodeId) {
      var ch = children(nodeId);
      var foundInChildren = ch.reduce(function (p, c) {
        return filterNode(c) || p;
      }, false);
      expanded[nodeId] = nodeId === rootId || foundInChildren;

      if (nodeId === rootId || predicate(nodeId)) {
        return true;
      } else {
        if (!foundInChildren) filteredOut[nodeId] = true;
        return foundInChildren;
      }
    }

    filterNode(rootId);
    return [filteredOut, expanded];
  };
};

exports.createFilter = createFilter;