import copy


def _convert_dashes(config, ref):
    """Convert dash in keys to underscores based on a reference dict.

    The reference is used to avoid converting keys in dictionary that are values
    of options.
    """
    config = copy.deepcopy(config)
    for key in list(config.keys()):
        converted_key = key.replace("-", "_")
        if converted_key in ref:
            config[converted_key] = config.pop(key)

            if all(isinstance(item[converted_key], dict) for item in [config, ref]):
                config[converted_key] = _convert_dashes(
                    config[converted_key], ref[converted_key]
                )

    return config