import ipaddress


def config_to_map(topology_config):
    """
    args:
        topology_config: dict

            {
            'region1': [
                    '10.1.1.0/24',
                    '10.1.10.0/24',
                    '172.16.1.0/24'
                ],
            'region2': [
                    '192.168.1.0/24',
                    '10.2.0.0/16',
                ]
            }

            Region cannot be "_default"

    returns:
        topology_map: dict
            {
                ip_network('10.1.1.0/24'): 'region1',
                ip_network('10.1.10.0/24'): 'region1',
                ip_network('172.16.1.0/24'): 'region1',

                ip_network('192.168.1.0/24'): 'region2',
                ip_network('10.2.0.0/16'): 'region2',
            }

    raises:
        ValueError: if a region value is "_default"
    """
    topology_map = {}
    for region in topology_config:
        # "_default" cannot be used as a region name
        if region == '_default':
            raise ValueError('cannot use "_default" as a region name')

        for net_str in topology_config[region]:
            net = ipaddress.ip_network(net_str)
            topology_map[net] = region

    return topology_map