# Path: social/utils.py
# def slugify(value):
#     """Converts to lowercase, removes non-word characters (alphanumerics
#     and underscores) and converts spaces to hyphens. Also strips leading
#     and trailing whitespace."""
#     value = unicodedata.normalize('NFKD', value) \
#                        .encode('ascii', 'ignore') \
#                        .decode('ascii')
#     value = re.sub('[^\w\s-]', '', value).strip().lower()
#     return re.sub('[-\s]+', '-', value)


from uuid import uuid4
from social.utils import slugify



def get_username(strategy, details, user=None, *args, **kwargs):
    storage = strategy.storage

    if not user:
        email_as_username = strategy.setting('USERNAME_IS_FULL_EMAIL', False)
        uuid_length = strategy.setting('UUID_LENGTH', 16)
        max_length = storage.user.username_max_length()
        do_slugify = strategy.setting('SLUGIFY_USERNAMES', False)
        do_clean = strategy.setting('CLEAN_USERNAMES', True)

        if email_as_username and details.get('email'):
            username = details['email']
        elif details.get('username'):
            username = details['username']
        else:
            username = uuid4().hex

        short_username = username[:max_length - uuid_length]
        final_username = username[:max_length]
        if do_clean:
            final_username = storage.user.clean_username(final_username)
        if do_slugify:

# Path: social/utils.py
# def slugify(value):
#     """Converts to lowercase, removes non-word characters (alphanumerics
#     and underscores) and converts spaces to hyphens. Also strips leading
#     and trailing whitespace."""
#     value = unicodedata.normalize('NFKD', value) \
#                        .encode('ascii', 'ignore') \
#                        .decode('ascii')
#     value = re.sub('[^\w\s-]', '', value).strip().lower()
#     return re.sub('[-\s]+', '-', value)


# Path: social/pipeline/user.py
from uuid import uuid4
from social.utils import slugify




def get_username(strategy, details, user=None, *args, **kwargs):
    storage = strategy.storage

    if not user:
        email_as_username = strategy.setting('USERNAME_IS_FULL_EMAIL', False)
        uuid_length = strategy.setting('UUID_LENGTH', 16)
        max_length = storage.user.username_max_length()
        do_slugify = strategy.setting('SLUGIFY_USERNAMES', False)
        do_clean = strategy.setting('CLEAN_USERNAMES', True)

        if email_as_username and details.get('email'):
            username = details['email']
        elif details.get('username'):
            username = details['username']
        else:
            username = uuid4().hex

        short_username = username[:max_length - uuid_length]
        final_username = username[:max_length]
        if do_clean:
            final_username = storage.user.clean_username(final_username)
        if do_slugify:

            final_username = slugify(final_username)