from typing import List
from typing import Dict


def build_url_filter(
        excluded_fields: List[str],
        included_fields: List[str]
) -> Dict[str, Dict[str, bool]]:
    """Return a URL filter based on included and excluded fields.

    If a field appears in both excluded_fields and included_fields, it
    is ultimately included.

    Args:
        excluded_fields (list[str]): names of the fields to explicitly
            exclude from the result.
        included_fields (list[str]): names of the fields to explicitly
            include in the result.

    Returns:
        dict: the query, as a dict in the format for the API.
    """
    excluded_fields = excluded_fields or []
    included_fields = included_fields or []
    field_flags = {}
    ret = {}

    # Build a map of fields to bool.
    for field_ in excluded_fields:
        field_flags[field_] = False
    for field_ in included_fields:
        # Set the included fields. If a field_ here was also in
        # excluded_fields, it is overwritten here.
        field_flags[field_] = True

    if 'properties' in field_flags:
        field_flags['calibration'] = field_flags.pop('properties')

    if field_flags:
        ret = {'fields': field_flags}

    return ret