from typing import List
from pathlib import Path
from typing import Tuple


def match_images_to_labels_and_bounds(
    images: List[Path], trace_labels: List[Path], bounds: List[Path]
) -> List[Tuple[Path, Path, Path]]:
    """
    Match image files to labels (traces) and bounds (target areas).

    Used to match the images in training/validation folder to
    corresponding traces (labels) and areas (bounds).

    >>> images = [Path("kl5.png")]
    >>> trace_labels = [Path("kl5_traces.shp")]
    >>> bounds = [Path("kl5_area.shp")]
    >>> results = match_images_to_labels_and_bounds(images, trace_labels, bounds)
    >>> print(list(map(str, results[0])))
    ['kl5.png', 'kl5_traces.shp', 'kl5_area.shp']

    With faulty image name:

    >>> images = [Path("kl4.png")]
    >>> trace_labels = [Path("kl5_traces.shp")]
    >>> bounds = [Path("kl5_area.shp")]
    >>> match_images_to_labels_and_bounds(images, trace_labels, bounds)
    []

    Does not have to be ESRI Shapefiles.

    >>> images = [Path("kl5.png")]
    >>> trace_labels = [Path("kl5_traces.geojson")]
    >>> bounds = [Path("kl5_area.geojson")]
    >>> results = match_images_to_labels_and_bounds(images, trace_labels, bounds)
    >>> print(list(map(str, results[0])))
    ['kl5.png', 'kl5_traces.geojson', 'kl5_area.geojson']
    """
    target_list = []

    # Iterate over each image path
    for image in images:

        # Iterate over each traces path
        for label in trace_labels:
            # Iterate over each area path
            for bound in bounds:

                # Condition is as follows:
                # image name (stem) must be a subset of the names of traces and area
                if image.stem in label.name and image.stem in bound.name:
                    target_list.append((image, label, bound))
    return target_list