/**
 * MemoryProviderRegistry — Singleton registry for MemoryProvider instances.
 *
 * Auto-registers LocalMemoryProvider as the default provider on first access.
 * External providers (Supermemory, Zep, Graphiti) can be registered at runtime.
 *
 * Usage:
 *   const registry = MemoryProviderRegistry.getInstance();
 *   const local = registry.getDefault();
 *   await local.connect({ userId: "user-123" });
 *   const id = await local.store({ content: "Meeting notes..." });
 */
import { LocalMemoryProvider } from "./localMemoryProvider.js";
// ═══════════════════════════════════════════════════════════════════════════
// Registry
// ═══════════════════════════════════════════════════════════════════════════
const DEFAULT_PROVIDER_NAME = "local-sqlite";
export class MemoryProviderRegistry {
    static instance = null;
    providers = new Map();
    defaultName = DEFAULT_PROVIDER_NAME;
    constructor() {
        // Auto-register LocalMemoryProvider as the default
        const local = new LocalMemoryProvider();
        this.providers.set(local.name, local);
    }
    // ── Singleton access ───────────────────────────────────────────────────
    static getInstance() {
        if (!MemoryProviderRegistry.instance) {
            MemoryProviderRegistry.instance = new MemoryProviderRegistry();
        }
        return MemoryProviderRegistry.instance;
    }
    /** Reset the singleton (for tests only) */
    static resetInstance() {
        MemoryProviderRegistry.instance = null;
    }
    // ── Registration ───────────────────────────────────────────────────────
    /**
     * Register a provider. Overwrites if a provider with the same name
     * already exists. Returns the registry for chaining.
     */
    register(provider) {
        this.providers.set(provider.name, provider);
        return this;
    }
    /**
     * Unregister a provider by name. Cannot unregister the default provider
     * unless another default is set first.
     */
    unregister(name) {
        if (name === this.defaultName && this.providers.size > 1) {
            throw new Error(`Cannot unregister default provider "${name}" — set a new default first`);
        }
        return this.providers.delete(name);
    }
    /**
     * Set which registered provider is the default.
     */
    setDefault(name) {
        if (!this.providers.has(name)) {
            throw new Error(`Provider "${name}" is not registered`);
        }
        this.defaultName = name;
    }
    // ── Retrieval ──────────────────────────────────────────────────────────
    /**
     * Get a provider by name. Returns undefined if not found.
     */
    get(name) {
        return this.providers.get(name);
    }
    /**
     * Get the default provider (LocalMemoryProvider unless changed).
     */
    getDefault() {
        const provider = this.providers.get(this.defaultName);
        if (!provider) {
            throw new Error(`Default provider "${this.defaultName}" not found — registry may be corrupted`);
        }
        return provider;
    }
    /**
     * List all registered providers with their connection status.
     */
    listProviders() {
        return [...this.providers.values()].map((p) => ({
            name: p.name,
            type: p.type,
            isDefault: p.name === this.defaultName,
            isConnected: p.isConnected(),
        }));
    }
    /**
     * Get the count of registered providers.
     */
    get size() {
        return this.providers.size;
    }
    /**
     * Check if a provider is registered.
     */
    has(name) {
        return this.providers.has(name);
    }
}
//# sourceMappingURL=memoryProviderRegistry.js.map