from typing import List
from typing import Optional


def _get_array_num_elements(array_dims: List[int],
                            index_depth: Optional[int] = None):
  """Returns the number of elements in a (nested) array.

  Returns the number of elements in a (nested) array with dimensions
  'array_dims'.  If the array is indexed 'index_depth' times. If 'index_depth'
  is not specified, the maximum number of possible indices is assumed.

  Args:
    array_dims: Array dimensions.
    index_depth: Depth of index.

  Returns:
    The number of elements in a (nested) array with dimensions 'array_dims'.

  """
  if index_depth is None:
    index_depth = len(array_dims) - 1

  elem_count = 1
  for idx in range(len(array_dims) - index_depth, len(array_dims)):
    elem_count = elem_count * array_dims[idx]
  return elem_count