from typing import Dict


def human_friendly_name_to_canonical_action_name(
        human_friendly_action_name: str,
        action_base_name_to_default_mode: Dict[str, str]):
    """
    Converts a human-friendly action name (used in the spreadsheet) and turns
    into the format compatible with this testing framework. This does two
    things:
    1) Resolving specified modes, from "()" format into a "_". For example,
       "action(with_mode)" turns into "action_with_mode".
    2) Resolving modeless actions (that have a default mode) to
       include the default mode. For example, if
       |action_base_name_to_default_mode| contains an entry for
       |human_friendly_action_name|, then that entry is appended to the action.
       "action" and {"action": "default_mode"} will respectively will return
       "action_default_mode".
    If neither of those cases apply, then the |human_friendly_action_name| is
    returned.
    """
    human_friendly_action_name = human_friendly_action_name.strip()
    if human_friendly_action_name in action_base_name_to_default_mode:
        # Handle default mode.
        human_friendly_action_name += "_" + action_base_name_to_default_mode[
            human_friendly_action_name]
    elif '(' in human_friendly_action_name:
        # Handle mode being specified.
        human_friendly_action_name = human_friendly_action_name.replace(
            "(", "_").rstrip(")")
    return human_friendly_action_name