from typing import List
from typing import Optional
from typing import Tuple
from typing import Dict


def make_dataset(
        paths: List[str],
        extensions: Optional[Tuple[str, ...]] = None,
) -> Tuple[List[Tuple[str, int]], Dict[bytes, int]]:
    """
    Map folder+classnames into list of (imagepath, class_index). Requires potentially
    expensive glob filesystem. For large object store datasets it's recommended to cache this.
    """
    instances = []
    counter = 0
    if extensions is None:
        extensions = ()
    # make it easier to directly match on result of str.split
    ext_set = set(ext[1:].lower() for ext in extensions)
    classes_to_idx = {}
    for path in sorted(paths):
        components = path.split('/')
        # based on above glob expression the last 2 components are filename/class
        potentialclass = components[-2].encode('utf-8')
        fname = components[-1]
        if fname.split('.')[-1].lower() not in ext_set:
            continue
        if potentialclass not in classes_to_idx:
            classes_to_idx[potentialclass] = counter
            counter += 1
        instances.append((path, classes_to_idx[potentialclass]))
    return instances, classes_to_idx