from typing import Tuple
from typing import Dict
from typing import Union
from typing import List


def parse_stan_vars(
    names: Tuple[str, ...]
) -> Tuple[Dict[str, Tuple[int, ...]], Dict[str, Tuple[int, ...]]]:
    """
    Parses out Stan variable names (i.e., names not ending in `__`)
    from list of CSV file column names.
    Returns a pair of dicts which map variable names to dimensions and
    variable names to columns, respectively, using zero-based column indexing.
    Note: assumes: (a) munged varnames and (b) container vars are non-ragged
    and dense; no checks size, indices.
    """
    if names is None:
        raise ValueError('missing argument "names"')
    dims_map: Dict[str, Tuple[int, ...]] = {}
    cols_map: Dict[str, Tuple[int, ...]] = {}
    idxs = []
    dims: Union[List[str], List[int]]
    for (idx, name) in enumerate(names):
        idxs.append(idx)
        var, *dims = name.split('[')
        if var.endswith('__'):
            idxs = []
        elif len(dims) == 0:
            dims_map[var] = ()
            cols_map[var] = tuple(idxs)
            idxs = []
        else:
            if idx < len(names) - 1 and names[idx + 1].split('[')[0] == var:
                continue
            dims = [int(x) for x in dims[0][:-1].split(',')]
            dims_map[var] = tuple(dims)
            cols_map[var] = tuple(idxs)
            idxs = []
    return (dims_map, cols_map)