package com.izouma.immall.security;

import com.izouma.immall.domain.BackUser;
import com.izouma.immall.domain.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public final class JwtBackUserFactory {

    private JwtBackUserFactory() {
    }

    public static JwtBackUser create(BackUser user) {
        return new JwtBackUser(user, mapToGrantedAuthorities(user.getAuthorities()));
    }

    private static List<GrantedAuthority> mapToGrantedAuthorities(Set<Authority> authorities) {
        if (authorities != null) {
            return authorities.stream()
                              .map(authority -> new SimpleGrantedAuthority(authority.getName()))
                              .collect(Collectors.toList());
        }
        return null;
    }
}
