package org.xyzh.auth.strategy;

import org.springframework.stereotype.Component;
import org.xyzh.common.exception.auth.AuthException;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @description    LoginStrategyFactory.java文件描述 登录策略工厂
 * @filename    LoginStrategyFactory.java
 * @author    yslg
 * @copyright xyzh
 * @since 2025-09-28
 */
@Component
public class LoginStrategyFactory {

    private final Map<String, LoginStrategy> strategies;

    public LoginStrategyFactory(List<LoginStrategy> loginStrategies) {
        this.strategies = loginStrategies.stream()
                .collect(Collectors.toMap(LoginStrategy::getLoginType, Function.identity()));
    }

    /**
     * @description    获取登录策略
     * @param    loginType 登录类型
     * @return    LoginStrategy 登录策略
     * @author    yslg
     * @since 2025-09-28
     */
    public LoginStrategy getStrategy(String loginType) {
        LoginStrategy strategy = strategies.get(loginType);
        if (strategy == null) {
            throw new AuthException("UNSUPPORTED_LOGIN_TYPE", "不支持的登录类型: " + loginType);
        }
        return strategy;
    }

    /**
     * @description    获取所有支持的登录类型
     * @return    Set<String> 登录类型集合
     * @author    yslg
     * @since 2025-09-28
     */
    public java.util.Set<String> getSupportedLoginTypes() {
        return strategies.keySet();
    }
}