import re
import itertools
import random


def generate_spintax(text, single=True):
    """Return a list of unique spins of a Spintax text string.

    Args:
        text (string): Spintax text (i.e. I am the {President|King|Ambassador} of Nigeria.)
        single (bool, optional): Optional boolean to return a list or a single spin.

    Returns:
        spins (string, list): Single spin or list of spins depending on single.
    """

    pattern = re.compile('({[^}]+}|[^{}]*)')
    chunks = pattern.split(text)

    def options(s):
        if len(s) > 0 and s[0] == '{':
            return [opt for opt in s[1:-1].split('|')]
        return [s]

    parts_list = [options(chunk) for chunk in chunks]

    spins = []

    for spin in itertools.product(*parts_list):
        spins.append(''.join(spin))

    if single:
        return spins[random.randint(0, len(spins) - 1)]
    else:
        return spins