from typing import Optional
from typing import Tuple


def check_interval_included(element1: dict, element2: dict) -> Optional[Tuple[dict, dict]]:
    """
    Comparison of two entities on start and end positions to find if they are nested

    Parameters
    ----------
    element1 : dict
    element2 : dict
        both of them in the following format
        {
            'entity': str,
            'word': str,
            'startCharIndex': int,
            'endCharIndex': int
        }

    Returns
    -------
    If there is an entity to remove among the two returns a tuple (element to remove, element to keep)
    If not, returns None
    """
    if ((element1 != element2) and (element1['startCharIndex'] >= element2['startCharIndex']) and
            (element1['endCharIndex'] <= element2['endCharIndex'])):
        return element1, element2
    if ((element1 != element2) and (element2['startCharIndex'] >= element1['startCharIndex']) and
            (element2['endCharIndex'] <= element1['endCharIndex'])):
        return element2, element1
    if ((element1 != element2) and (element1['startCharIndex'] >= element2['startCharIndex']) and
            (element1['endCharIndex'] >= element2['endCharIndex']) and
            (element1['startCharIndex'] <= element2['endCharIndex']-1)):
        return element1, element2
    if ((element1 != element2) and (element2['startCharIndex'] >= element1['startCharIndex']) and
            (element2['endCharIndex'] >= element1['endCharIndex']) and
            (element2['startCharIndex'] < element1['endCharIndex']-1)):
        return element2, element1
    return None