from typing import Tuple
from typing import List


def _unpack_term(
    term_str: str, expand_number_op: bool = False
) -> Tuple[Tuple[int, int, int], List[Tuple[int, str]]]:
    """Return a tuple specifying the counts of kinds of operators in `term_str` and
    a list of the factors and their indices in `term_str`.

    The factors are represented by tuples of the form `(i, c)`, where `i` is an index
    and `c` is a character.
    Allowed characters in `term_str` are 'N+-I`.
    The returned tuple contains counts for `N`, `+`, and `-`, in that order. Identity operators
    are ignored.

    Args:
       term_str: a string of characters in `N+-I`.
       expand_number_op: if `True`, number operators are expanded to `(i, '+')`, `(i, '-')`
         in the returned list of factors.

    Returns:
      tuple: A tuple of two elements. First, a tuple of three integers giving the number of
      number, raising, and lowering operators. Second a list of factors represented by
      tuples of two elements: the first is an index and the second one of "-", "+", or "N".
      If `expand_number_op` is `True`, then factors of `N` are expanded.
    """  # pylint: disable=missing-raises-doc
    (n_number, n_raise, n_lower) = (0, 0, 0)
    facs = []
    for i, c in enumerate(term_str):
        if c == "I":
            continue
        if c == "+":
            n_raise += 1
            facs.append((i, "+"))
        elif c == "-":
            n_lower += 1
            facs.append((i, "-"))
        elif c == "N":
            n_number += 1
            if expand_number_op:
                facs.append((i, "+"))
                facs.append((i, "-"))
            else:
                facs.append((i, "N"))
        else:
            raise ValueError("Unsupported operator ", c, " in term.")

    return (n_number, n_raise, n_lower), facs