import logging


def select_best_haplotype_match(all_matches):
  """Returns the best HaplotypeMatch among all_matches.

  The best matching HaplotypeMatch is the one with the lowest match_metrics
  score.

  Args:
    all_matches: iterable[HaplotypeMatch]. An iterable of HaplotypeMatch objects
      we want to select the best match from.

  Returns:
    The best matching HaplotypeMatch object.
  """
  sorted_matches = sorted(all_matches, key=lambda x: x.match_metrics)
  best = sorted_matches[0]
  equivalents = [
      f for f in all_matches if f.match_metrics == best.match_metrics
  ]

  # redacted
  if len(equivalents) > 1:
    for i, f in enumerate(equivalents):
      extra_info = 'best' if i == 0 else i
      logging.warning('Equivalent match to best: %s [%s]', f, extra_info)

  return equivalents[0]