from typing import Tuple
import re


def format_header(header: str) -> Tuple[str, int, str]:
    """Calculates the level of the header, removes leading and trailing whitespaces and creates the markdown-link.

    :param header: header line from the markdown file
    :return: a tuple consisting of the cleaned header, the header level and the formatted markdown link.
    """

    # determine the level of the header
    level = 0
    while header[0] == "#":
        level += 1
        header = header[1:]

    # create clickable link by allowing only certain characters,
    # by replacing whitespaces with hyphens and by removing colons
    headerlink = "#" + re.sub(r"[^a-zA-Z0-9 -]", "", header).lower().strip().replace(
        " ", "-"
    ).replace("--", "-")
    return (header.strip(), level, headerlink)