export function selectContentEditable(element) {
  element.focus(); // Move the cursor to the end.

  var selection = window.getSelection();

  if (selection) {
    selection.removeAllRanges();
    var range = document.createRange();
    range.selectNodeContents(element);
    range.collapse(false);
    selection.addRange(range);
  }
}

function filterArraySuggestions(filter, suggestions) {
  var lowerFilter = filter.toLowerCase();
  return suggestions.filter(function (suggestion) {
    var _suggestion$label;

    return suggestion.email !== filter && (suggestion.email.toLowerCase().includes(lowerFilter) || ((_suggestion$label = suggestion.label) === null || _suggestion$label === void 0 ? void 0 : _suggestion$label.toLowerCase().includes(lowerFilter)));
  });
}

export function filterSuggestions(filter, suggestions) {
  var maxLength = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 5;

  if (Array.isArray(suggestions)) {
    return filterArraySuggestions(filter, suggestions).slice(0, maxLength);
  }

  var filteredSuggestions = {};

  for (var key in suggestions) {
    var result = filterArraySuggestions(filter, suggestions[key]).slice(0, maxLength);

    if (result.length > 0) {
      filteredSuggestions[key] = result;
    }
  }

  return filteredSuggestions;
}

function excludeArraySuggestions(filter, suggestions) {
  return suggestions.filter(function (suggestion) {
    return !filter.includes(suggestion.email);
  });
}

export function excludeSuggestions(filter, suggestions) {
  var emails = filter.map(function (option) {
    return option.email;
  });

  if (Array.isArray(suggestions)) {
    return excludeArraySuggestions(emails, suggestions);
  }

  var filteredSuggestions = {};

  for (var key in suggestions) {
    var result = excludeArraySuggestions(emails, suggestions[key]);

    if (result.length > 0) {
      filteredSuggestions[key] = result;
    }
  }

  return filteredSuggestions;
}
export function validateSuggestion(filter, suggestion) {
  var _suggestion$label$toL, _suggestion$label2;

  var lowerFilter = filter.toLowerCase();
  return suggestion.email !== filter && (suggestion.email.toLowerCase().includes(lowerFilter) || ((_suggestion$label$toL = (_suggestion$label2 = suggestion.label) === null || _suggestion$label2 === void 0 ? void 0 : _suggestion$label2.toLowerCase().includes(lowerFilter)) !== null && _suggestion$label$toL !== void 0 ? _suggestion$label$toL : false));
}
export function moveSuggestion(suggestions, selectedSuggestion, moveBy) {
  var _group2;

  if (Array.isArray(suggestions)) {
    var _suggestions;

    var index = selectedSuggestion ? suggestions.indexOf(selectedSuggestion) : -1;

    if (suggestions[index + moveBy]) {
      return suggestions[index + moveBy];
    }

    return (_suggestions = suggestions[moveBy > 0 ? 0 : suggestions.length - 1]) !== null && _suggestions !== void 0 ? _suggestions : null;
  }

  var keys = Object.keys(suggestions);

  if (keys.length === 0) {
    return null;
  }

  for (var i = 0; i < keys.length; i++) {
    var _group = suggestions[keys[i]];

    var _index = selectedSuggestion ? _group.indexOf(selectedSuggestion) : -1;

    if (_index === -1) {
      continue;
    }

    if (_group[_index + moveBy]) {
      return _group[_index + moveBy];
    }

    var nextGroup = suggestions[keys[i + moveBy]];

    if (nextGroup) {
      return nextGroup[moveBy > 0 ? 0 : nextGroup.length - 1];
    }

    break;
  }

  var group = suggestions[keys[moveBy > 0 ? 0 : keys.length - 1]];
  return (_group2 = group[moveBy > 0 ? 0 : group.length - 1]) !== null && _group2 !== void 0 ? _group2 : null;
}