/**
 * Adapter 注册表 —— 管理 Provider ID → Adapter 实例的映射
 */
/**
 * Adapter 注册表
 * 管理所有 Provider Adapter 实例的生命周期
 */
export class AdapterRegistry {
    constructor() {
        this.adapters = new Map();
    }
    /**
     * 注册 Adapter 实例
     * @param adapter Adapter 实例（providerId 作为 key）
     * @throws 如果该 providerId 已注册
     */
    register(adapter) {
        if (this.adapters.has(adapter.providerId)) {
            throw new Error(`Adapter already registered for provider: ${adapter.providerId}`);
        }
        this.adapters.set(adapter.providerId, adapter);
    }
    /**
     * 按 Provider ID 获取 Adapter
     * @param providerId Provider 标识（如 'claude-code', 'opencode'）
     * @throws 如果该 providerId 未注册
     */
    get(providerId) {
        const adapter = this.adapters.get(providerId);
        if (!adapter) {
            throw new Error(`No adapter registered for provider: ${providerId}. Available: ${[...this.adapters.keys()].join(', ')}`);
        }
        return adapter;
    }
    /**
     * 按 AdapterType 获取 Adapter
     * @param adapterType 适配器类型
     * @throws 如果该类型未注册
     */
    getByType(adapterType) {
        for (const adapter of this.adapters.values()) {
            if (this.matchType(adapter.providerId, adapterType)) {
                return adapter;
            }
        }
        throw new Error(`No adapter registered for type: ${adapterType}`);
    }
    /**
     * 检查是否已注册
     * @param providerId Provider 标识
     */
    has(providerId) {
        return this.adapters.has(providerId);
    }
    /**
     * 获取所有已注册的 Provider ID
     */
    getRegisteredIds() {
        return [...this.adapters.keys()];
    }
    /**
     * 获取所有已注册 Adapter。
     */
    getAll() {
        return [...this.adapters.values()];
    }
    /**
     * 清理所有 Adapter 资源
     */
    cleanup() {
        for (const adapter of this.adapters.values()) {
            try {
                adapter.cleanup();
            }
            catch (err) {
                console.error(`[AdapterRegistry] Error cleaning up ${adapter.providerId}:`, err);
            }
        }
        this.adapters.clear();
    }
    /**
     * Provider ID → AdapterType 匹配
     */
    matchType(providerId, adapterType) {
        switch (adapterType) {
            case 'claude-sdk':
                return providerId === 'claude-code';
            case 'acode-sdk':
                return providerId === 'acode';
            case 'codex-sdk':
                return providerId === 'codex';
            case 'opencode-sdk':
                return providerId === 'opencode';
            default:
                return false;
        }
    }
}
// 单例实例
export const adapterRegistry = new AdapterRegistry();
