import re


def flair_sanitizer(text_to_parse, change_case=True):
    """This is a small function that sanitizes the input from the user
    for flairs and from flair dictionaries' text in order to make them
    consistent. This includes removing extraneous characters,
    lower-casing and stripping, and removing Reddit and Unicode emoji.

    :param text_to_parse: The text we want to convert and clean up.
    :param change_case: Whether or not we want to change the
                        capitalization of the text. Generally we want to
                        change it if it's for a case-insensitive
                        situation like matching people's messages.
                        Otherwise, if we're just displaying the options
                        available, we *do not* want to change case.
    :return: The sanitized text.
    """
    # Here we REMOVE the brackets and characters that may be in post
    # flairs so that they can match.
    deleted_characters = ["[", "]", ">", "•"]
    for character in deleted_characters:
        if character in text_to_parse:
            text_to_parse = text_to_parse.replace(character, "")

    # Here we REPLACE some problematic characters that may cause
    # rendering issues, namely vertical pipes in tables.
    replaced_characters = {"|": "◦"}
    for character in replaced_characters:
        if character in text_to_parse:
            text_to_parse = text_to_parse.replace(character, replaced_characters[character])

    # Process the text further. If changing case is desired, change it.
    # In case people keep the Reddit emoji text in, delete it.
    text_to_parse = text_to_parse.strip()
    if change_case:
        text_to_parse = text_to_parse.lower()
    text_to_parse = re.sub(r":\S+:", "", text_to_parse)

    # Account for Unicode emoji by deleting them as well.
    # uFE0F is an invisible character marking emoji.
    reg = re.compile(
        u"[\U0001F300-\U0001F64F"
        u"\U0001F680-\U0001F6FF"
        u"\U0001F7E0-\U0001F7EF"
        u"\U0001F900-\U0001FA9F"
        u"\uFE0F\u2600-\u26FF\u2700-\u27BF]",
        re.UNICODE,
    )
    text_to_parse = reg.sub("", text_to_parse).strip()

    return text_to_parse