function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

/**
 * 
 * @file Suggested Collaborator utility functions
 * @author Box
 */
import fuzzySearch from '../../../utils/fuzzySearch';

function scoreComparator(optionA, optionB) {
  return optionB.userScore - optionA.userScore;
}
/**
 * Function to compute suggested collaborators given a list of contacts and cached suggested collaborators.
 * Used by input components to help augment API results with cached suggestions using a fuzzy search.
 */


function computeSuggestedCollabs(contacts, suggestedCollabLookup, inputValue) {
  var maxSuggestions = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 3;
  var minCharacters = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 3;
  var maxGaps = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 2;
  var contactIdSet = new Set(contacts.map(function (contact) {
    return contact.id.toString();
  })); // $FlowFixMe

  var suggestedCollabs = Object.values(suggestedCollabLookup);
  var exactMatches = suggestedCollabs.filter(function (suggestedCollab) {
    return suggestedCollab.id && contactIdSet.has(suggestedCollab.id.toString());
  });
  var exactMatchIds = exactMatches.map(function (suggestedCollab) {
    return suggestedCollab.id.toString();
  });
  var suggestedFuzzyMatches = exactMatches.length >= maxSuggestions ? [] : suggestedCollabs.filter(function (option) {
    if (!inputValue || exactMatchIds.includes(option.id.toString())) {
      return false;
    }

    var nameMatches = fuzzySearch(inputValue, option.name, minCharacters, maxGaps);
    var emailAddress = option.email || '';
    var emailAlias = emailAddress.substring(0, emailAddress.indexOf('@'));
    var emailMatches = inputValue.length >= minCharacters && emailAlias.includes(inputValue);
    return nameMatches || emailMatches;
  }); // combine both lists preferring exact id matches over fuzzy matches

  var suggestedResult = [].concat(_toConsumableArray(exactMatches.sort(scoreComparator)), _toConsumableArray(suggestedFuzzyMatches.sort(scoreComparator))).slice(0, maxSuggestions);
  var suggestedResultIds = suggestedResult.map(function (suggestion) {
    return suggestion.id;
  });
  var otherResults = contacts.filter(function (contact) {
    return !suggestedResultIds.includes(contact.id);
  });
  return [suggestedResult, otherResults];
}

export default computeSuggestedCollabs;
//# sourceMappingURL=computeSuggestedCollabs.js.map