from typing import Set
from typing import Tuple
from typing import Dict


def _valid_bond(used_partitions: Set[int], bond: Tuple[int, int],
                current_partition: int,
                comp_map: Dict[int, int]) -> Tuple[bool, int]:
  """Helper method to find next partition to explore.

  Used to check if a bond goes from the current partition into a
  partition that is not yet explored

  Parameters
  ----------
  used_partions: Set[int]
    Partitions which have already been used
  bond: Tuple[int, int]
    The bond to check if it goes to an unexplored partition.
    This tuple is (from_atom, to_atom).
  current_partition: int
    The current partition of the DFS
  comp_map: Dict[int, int]
    Maps atom ids to component ids

  Returns
  -------
  is_valid: bool
    Whether to exist the next partition or not
  next_partition: int
    The next partition to explore
  """
  part1 = comp_map[bond[0]]
  part2 = comp_map[bond[1]]
  if part1 != current_partition and part2 != current_partition:
    return False, 0
  if part1 == current_partition:
    next_partition = part2
  else:
    next_partition = part1
  return next_partition not in used_partitions, next_partition