from typing import Dict
from typing import Optional


def get_new_name(attrs: Dict[str, str],
                 mark_name: str = '',
                 name_map: Optional[Dict[str, str]] = None) -> str:
    """Get new name for a node.

    Args:
        attrs (Dict[str, str]): A dict contains attributes of an ONNX node.
        mark_name (str): The input mark op name. Default is ''.
        name_map (Dict[str, str]): A mapping of node names, defaults to
            `None`.

    Returns:
        str: The new node name.
    """
    if 'name' in attrs:
        new_name = attrs['name']
    else:
        new_name = '_'.join((attrs['func'], attrs['type'], str(attrs['id'])))

    if name_map is not None:
        if new_name in name_map:
            return name_map[new_name]

        if f'{mark_name}:{new_name}' in name_map:
            return name_map[f'{mark_name}:{new_name}']

    return new_name