/**
 * 数据源注册表
 * 集中管理所有预设数据源配置
 */
import { DataSourceType, type DataSourceConfig, type DataSourceRegistry } from './types';
import { BaseRegistry } from './core/BaseRegistry';
import {
  jenkinsServersConfig,
  k8sClustersConfig,
  gitRepositoriesConfig,
  dockerRegistriesConfig,
  environmentsConfig,
  projectGroupsConfig,
  applicationsConfig,
  notificationChannelTypesConfig,
  notificationChannelsConfig,
  notificationTemplatesConfig,
  usersConfig,
  rolesConfig,
  departmentsConfig
} from './presets/infrastructure';

/**
 * 数据源注册表实例
 */
class DataSourceRegistryImpl extends BaseRegistry<DataSourceType, DataSourceConfig> {
  constructor() {
    super();
    // 注册所有预设配置
    this.register(DataSourceType.JENKINS_SERVERS, jenkinsServersConfig);
    this.register(DataSourceType.K8S_CLUSTERS, k8sClustersConfig);
    this.register(DataSourceType.GIT_REPOSITORIES, gitRepositoriesConfig);
    this.register(DataSourceType.DOCKER_REGISTRIES, dockerRegistriesConfig);
    this.register(DataSourceType.NOTIFICATION_CHANNEL_TYPES, notificationChannelTypesConfig);
    this.register(DataSourceType.NOTIFICATION_CHANNELS, notificationChannelsConfig);
    this.register(DataSourceType.NOTIFICATION_TEMPLATES, notificationTemplatesConfig);
    this.register(DataSourceType.USERS, usersConfig);
    this.register(DataSourceType.ROLES, rolesConfig);
    this.register(DataSourceType.DEPARTMENTS, departmentsConfig);
    this.register(DataSourceType.ENVIRONMENTS, environmentsConfig);
    this.register(DataSourceType.PROJECT_GROUPS, projectGroupsConfig);
    this.register(DataSourceType.APPLICATIONS, applicationsConfig);
  }

  /**
   * 获取注册表对象（向后兼容）
   */
  toObject(): DataSourceRegistry {
    return this['registry'] as DataSourceRegistry;
  }
}

const registryInstance = new DataSourceRegistryImpl();

/**
 * 数据源配置注册表（向后兼容）
 */
export const DATA_SOURCE_REGISTRY: DataSourceRegistry = registryInstance.toObject();

/**
 * 获取数据源配置
 */
export const getDataSourceConfig = (type: DataSourceType) => registryInstance.get(type);

/**
 * 检查数据源类型是否已注册
 */
export const hasDataSource = (type: DataSourceType): boolean => registryInstance.has(type);

/**
 * 获取所有已注册的数据源类型
 */
export const getAllDataSourceTypes = (): DataSourceType[] => registryInstance.getAllKeys();

