package com.izouma.nineth.security;

import com.izouma.nineth.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 JwtUserFactory {

    private JwtUserFactory() {
    }

    public static JwtUser create(User user) {
        return new JwtUser(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;
    }
}
