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


def match_initial(string: str, candidates: List[str]) -> Tuple[str, Optional[str]]:
    """
    Returns the longest match at the initial position among a list of candidates.

    The function will check if any candidate among the list is found at the beginning
    of the string, making sure to match longer candidates first (so that it will
    match "abc" before "ab").

    @param string: The string to matched at the beginning.
    @param candidates: A list of string candidates for initial match. The list does not
        need to be sorted in any way.
    @return: A tuple, whose first element is the original string stripped of the initial
        match, if any (if no candidate matched the beginning of the string, it will be
        a copy of the original one). The second element of the tuple is the candidate
        that was matched at the beginning of the string, or `None` if no match could be
        found.
    """

    # Sort the candidates by inverse length -- this is a bit expensive computationally,
    # but it is better to perform it each time to make the function more general.
    # Note that we make sure to remove any empty string inadvertedly passed among the
    # `candidates`.
    candidates = sorted([cand for cand in candidates if cand], reverse=True, key=len)
    for cand in candidates:
        if string.startswith(cand):
            return string[len(cand) :], cand

    return string, None