import unicodedata


def normalize_grapheme(grapheme, bipa):
    """
    Normalizes a grapheme.

    Does Unicode normalization, splitting in case of Phoible/slash-notation,
    BIPA default selection.
    """

    # Unicode normalization
    grapheme = unicodedata.normalize("NFC", grapheme)

    # Split over slash notation, if any, keeping the entry to the right
    if "/" in grapheme:
        grapheme = grapheme.split("/")[1]

    # Only split the vertical bar, as used in Phoible, if the grapheme is
    # longer than one character (can be a click), keeping the first one
    if len(grapheme) > 1 and "|" in grapheme:
        grapheme = grapheme.split("|")[0]

    # Normalize BIPA
    grapheme = str(bipa[grapheme])

    return grapheme