from typing import Union
import re
from typing import Optional


def compile_pattern(elements: Union[list[str], re.Pattern]) -> Optional[re.Pattern]:
    """
    compile a list of str to `|` sepreated regex pattern
    """
    if not elements:
        return None
    elif isinstance(elements, re.Pattern):
        return elements
    if isinstance(elements, (list, tuple)):
        return re.compile("|".join([re.escape(x.strip()) for x in elements]), re.U)
    else:
        raise Exception("Unknown type for the pattern: {}".format(type(elements)))