from typing import Union
from functools import reduce


def _process_crawlers(crawler_list: list, s3_location: str) -> Union[str, None]:
    """
    Given a list of crawlers, it will check their targets to see if they contain the provided s3 location.
    If they do, it will return the first one.
    :param crawler_list: List of crawlers
    :param s3_location: S3 path to be crawled
    :return: Name of the first encountered crawler that has the path on its targets
    """
    def _filter_crawler(crawler: dict) -> bool:
        """
        Returns true if s3_location is in the crawler's targets
        :param crawler: Crawler to be filtered
        :return: True if s3_location is in the crawler's targets, False otherwise
        """
        targets = list(map(lambda s3_target: s3_target['Path'], crawler['Targets']['S3Targets']))
        return s3_location in targets

    filtered_crawlers = list(filter(_filter_crawler, crawler_list))
    crawler_names = list(map(lambda crawler: crawler['Name'], filtered_crawlers))
    return reduce(lambda a, b: a, crawler_names) if len(crawler_names) > 0 else None