package tech.easyflow.datacenter.connector;

import org.springframework.stereotype.Component;
import tech.easyflow.common.web.exceptions.BusinessException;
import tech.easyflow.datacenter.meta.enums.DatacenterSourceType;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Component
public class DatacenterConnectorRegistry {

    private final Map<DatacenterSourceType, DatacenterConnector> connectorMap;

    public DatacenterConnectorRegistry(List<DatacenterConnector> connectors) {
        this.connectorMap = connectors.stream().collect(Collectors.toMap(DatacenterConnector::getSourceType, Function.identity()));
    }

    public DatacenterConnector getConnector(String sourceType) {
        DatacenterSourceType type = DatacenterSourceType.valueOf(sourceType);
        DatacenterConnector connector = connectorMap.get(type);
        if (connector == null) {
            throw new BusinessException("未找到连接器: " + sourceType);
        }
        return connector;
    }
}
