from typing import List
from typing import Dict
from typing import Any


def select_subset_of_overlapping_chain(
    chain: List[Dict[str, Any]]
) -> List[Dict[str, Any]]:
    """
    Select the subset of entities in an overlapping chain to return by greedily choosing the
    longest entity in the chain until there are no entities remaining
    """
    sorted_chain = sorted(chain, key=lambda x: x['end'] - x['start'], reverse=True)
    selections_from_chain = []
    chain_index = 0
    # dump the current chain by greedily keeping the longest entity that doesn't overlap
    while chain_index < len(sorted_chain):
        entity = sorted_chain[chain_index]
        match_found = False
        for already_selected_entity in selections_from_chain:
            max_start = max(entity['start'], already_selected_entity['start'])
            min_end = min(entity['end'], already_selected_entity['end'])
            if len(range(max_start, min_end)) > 0:
                match_found = True
                break

        if not match_found:
            selections_from_chain.append(entity)

        chain_index += 1

    return selections_from_chain