/**
 * 级联数据源注册表
 * 集中管理所有预设级联数据源配置
 */
import { CascadeDataSourceType, type CascadeDataSourceConfig, type CascadeDataSourceRegistry } from './types';
import { BaseRegistry } from './core/BaseRegistry';
import {
  environmentProjectsConfig,
  jenkinsServerViewsJobsConfig,
  departmentUsersConfig,
  departmentTreeConfig,
  projectGroupAppsConfig
} from './presets/cascade';

/**
 * 级联数据源注册表实例
 */
class CascadeDataSourceRegistryImpl extends BaseRegistry<CascadeDataSourceType, CascadeDataSourceConfig> {
  constructor() {
    super();
    // 注册所有预设配置
    this.register(CascadeDataSourceType.ENVIRONMENT_PROJECTS, environmentProjectsConfig);
    this.register(CascadeDataSourceType.JENKINS_SERVER_VIEWS_JOBS, jenkinsServerViewsJobsConfig);
    this.register(CascadeDataSourceType.DEPARTMENT_USERS, departmentUsersConfig);
    this.register(CascadeDataSourceType.DEPARTMENT_TREE, departmentTreeConfig);
    this.register(CascadeDataSourceType.PROJECT_GROUP_APPS, projectGroupAppsConfig);
  }

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

const registryInstance = new CascadeDataSourceRegistryImpl();

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

/**
 * 获取级联数据源配置
 */
export const getCascadeDataSourceConfig = (type: CascadeDataSourceType) => registryInstance.get(type);

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

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

