import logging
import copy


def graph_directional_to_undirectional(graph):
    """
    Convert a directional to an undirectional graph

    Returns a deep copy of the full graph with all directional edges
    duplicated as undirectional ones.
    Undirectional edges share the same data dictionary. In converting
    directional to undirectional edges their data dictionaries will
    be merged.

    .. Note:: dictionary merging may result in undesirable results due
              to data overwrite.

    :param graph: Graph to convert
    :type graph:  :graphit:Graph

    :return:      Directional graph
    :rtype:       :graphit:Graph
    """

    if not graph.directed:
        logging.info('Graph already configured as undirected graph')

    graph_copy = graph.copy(deep=True)
    graph_copy.directed = False
    graph_copy.edges.clear()

    done = []
    for edge in graph.edges:
        reverse_edge = tuple(reversed(edge))
        values = copy.deepcopy(graph.edges[edge])

        if edge in done or reverse_edge in done:
            continue

        if reverse_edge in graph.edges:
            values.update(graph.edges[reverse_edge])
            done.append(reverse_edge)

        graph_copy.add_edge(*edge, **values)
        done.append(edge)

    return graph_copy