from typing import Dict
from typing import List


def iterate_parent(ontology_label: str, ontology: Dict[str, List[str]],
                   family_tree: List[str], mappings: Dict[str, Dict[str, str]]):
    """
    Iterate ontology to find matched mapping.

    Parameters
    ----------
    ontology_label
        label to query mappings.

    ontology
        name to parents mappings.

    family_tree
        list of labels in the family tree.

    mappings
        ontology label to hub term/dimension mappings.

    Returns
    -------
    list of mapped dictionary of label, dimension, and hierarchy, for example iterating Manual segmentation returns
    [
        {
            "label": "Image Segmentation",
            "dimension": "Operation",
            "hierarchy": [
                "Image segmentation",
                "Manual segmentation"
            ]
        },
        {
            "label": "Image annotation",
            "dimension": "Operation",
            "hierarchy": [
                "Image annotation",
                "Dense image annotation",
                "Manual segmentation"
            ]
        }
    ]
    """
    family_tree.insert(0, ontology_label)
    if ontology_label in mappings:
        return [{
            "label": mappings[ontology_label]["label"],
            "dimension": mappings[ontology_label]["dimension"],
            "hierarchy": family_tree
        }]
    if ontology_label not in ontology:
        return []
    all_families = []
    for token in ontology[ontology_label]:
        all_families.extend(iterate_parent(token, ontology, family_tree.copy(), mappings))
    return all_families