from typing import List
from typing import Tuple


def create_toc(
        toc_levels: List[Tuple[str, int, str]], 
        level_limit: int) -> List[str]:
    """
    Creates a list of strings representing the items in the table of content.
    :param toc_levels:  A list of Tuples consisting of the header,
        the level of the header and a formatted markdown-link to the header.
                    Example for toc_levels:
                            [
                                ('First Header', 1, '#First-Header')
                                ('Second level', 2, '#Second-level')
                                ('First level again', 1, '#First-level-again')
                            ]
    :param level_limit: Limit to the number of levels included in the TOC
    :return: Ordered line items of the table of contents.
    """

    toc = ["# TOC"]
    # create a dict to store the header numbering for each level
    max_header_level = max([x[1] for x in toc_levels]) + 1
    headerlevels = dict.fromkeys(range(1, max_header_level), 1)
    previous_level = 1
    for i, (h, level, link) in enumerate(toc_levels):

        # reset lower header-levels if current header level is higher than prev
        if previous_level > level:
            for x in range(level + 1, previous_level + 1):
                headerlevels[x] = 1

        # construct TOC element
        if level <= level_limit:
            toc.append(
                "\t" * (level - 1) + f"{headerlevels[level]}. [" + 
                    h + f"]({link})"
            )

        # increment matching header level
        headerlevels[level] = headerlevels[level] + 1
        previous_level = level

    return toc