import collections


def parse_gtid_range_string(input_range):
    """Converts a string like "uuid1:id1-id2:id3:id4-id5, uuid2:id6" into a dict like
    {"uuid1": [[id1, id2], [id3, id3], [id4, id5]], "uuid2": [[id6, id6]]}. ID ranges are
    sorted from lowest to highest"""
    # This might be called again for a value that has already been processed
    if isinstance(input_range, dict):
        return input_range
    elif not input_range or not input_range.strip():
        return {}

    by_uuid = collections.defaultdict(list)
    for uuid_and_ranges in input_range.split(","):
        server_uuid, *ranges = uuid_and_ranges.strip().split(":")
        all_ranges = by_uuid[server_uuid.lower()]
        for rng in ranges:
            if "-" in rng:
                low, high = rng.split("-")
            else:
                low, high = rng, rng
            all_ranges.append([int(low), int(high)])
    return {server_uuid: sorted(ranges, key=lambda rng: rng[0]) for server_uuid, ranges in by_uuid.items()}