"use strict";
/**
 * Plugin Registry for Native AI
 *
 * This module provides a registry for discovering and loading plugins.
 * It includes built-in plugins and allows for dynamic loading of custom plugins.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.PluginRegistry = void 0;
const plugin_system_js_1 = require("./plugin-system.js");
const anthropic_provider_plugin_js_1 = require("../../examples/plugins/anthropic-provider-plugin.js");
const openai_provider_plugin_js_1 = require("../../examples/plugins/openai-provider-plugin.js");
/**
 * Plugin registry class
 */
class PluginRegistry {
    /**
     * Create a new plugin registry
     */
    constructor() {
        this.builtInPlugins = new Map();
        this.availablePlugins = new Map();
        this.loadedPlugins = new Map();
        // Register built-in plugins
        this.registerBuiltInPlugin('anthropic-provider', anthropic_provider_plugin_js_1.AnthropicProviderPlugin, {
            name: 'Anthropic Provider',
            description: 'Provider plugin for Anthropic Claude models',
            version: '1.0.0',
            type: 'provider',
            author: 'BFUI Team',
            enabled: true
        });
        this.registerBuiltInPlugin('openai-provider', openai_provider_plugin_js_1.OpenAIProviderPlugin, {
            name: 'OpenAI Provider',
            description: 'Provider plugin for OpenAI models',
            version: '1.0.0',
            type: 'provider',
            author: 'BFUI Team',
            enabled: true
        });
    }
    /**
     * Register a built-in plugin
     */
    registerBuiltInPlugin(id, pluginClass, metadata) {
        this.builtInPlugins.set(id, pluginClass);
        this.availablePlugins.set(id, {
            id,
            ...metadata
        });
    }
    /**
     * Get available plugins metadata
     */
    getAvailablePlugins() {
        return Array.from(this.availablePlugins.values());
    }
    /**
     * Get a plugin by ID
     */
    getPlugin(id) {
        return this.loadedPlugins.get(id);
    }
    /**
     * Load a plugin by ID
     */
    async loadPlugin(id) {
        // Check if plugin is already loaded
        const loadedPlugin = this.loadedPlugins.get(id);
        if (loadedPlugin) {
            return loadedPlugin;
        }
        // Check if plugin is available
        const metadata = this.availablePlugins.get(id);
        if (!metadata) {
            throw new Error(`Plugin not found: ${id}`);
        }
        // Check if plugin is enabled
        if (!metadata.enabled) {
            throw new Error(`Plugin is disabled: ${id}`);
        }
        // Check if plugin is built-in
        const PluginClass = this.builtInPlugins.get(id);
        if (!PluginClass) {
            throw new Error(`Plugin is not built-in and dynamic loading is not supported yet: ${id}`);
        }
        // Create plugin instance
        const plugin = new PluginClass();
        // Register plugin with the plugin manager
        plugin_system_js_1.pluginManager.registerPlugin(plugin);
        // Add to loaded plugins
        this.loadedPlugins.set(id, plugin);
        return plugin;
    }
    /**
     * Load all enabled plugins
     */
    async loadAllEnabledPlugins() {
        const enabledPluginIds = Array.from(this.availablePlugins.entries())
            .filter(([_, metadata]) => metadata.enabled)
            .map(([id]) => id);
        const loadedPlugins = [];
        for (const id of enabledPluginIds) {
            try {
                const plugin = await this.loadPlugin(id);
                loadedPlugins.push(plugin);
            }
            catch (error) {
                console.error(`Failed to load plugin ${id}:`, error);
            }
        }
        return loadedPlugins;
    }
}
exports.PluginRegistry = PluginRegistry;
//# sourceMappingURL=plugin-registry.js.map