"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PluginManager = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
/**
 * Plugin Manager Component
 *
 * This component displays all available plugins and allows users to enable/disable them.
 */
const react_1 = require("react");
const plugin_registry_js_1 = require("../../services/native-ai/plugin-registry.js");
const useNativeAI_js_1 = require("../../hooks/useNativeAI.js");
const PluginManager = ({ onPluginChange }) => {
    const [availablePlugins, setAvailablePlugins] = (0, react_1.useState)([]);
    const [loading, setLoading] = (0, react_1.useState)(true);
    const [error, setError] = (0, react_1.useState)(null);
    const { initialized } = (0, useNativeAI_js_1.useNativeAI)();
    // Load available plugins
    (0, react_1.useEffect)(() => {
        const loadPlugins = async () => {
            try {
                setLoading(true);
                if (!initialized) {
                    // Wait for Native AI to initialize
                    return;
                }
                // Get available plugins
                const plugins = plugin_registry_js_1.pluginRegistry.getAvailablePlugins();
                setAvailablePlugins(plugins);
                // Load all enabled plugins
                await plugin_registry_js_1.pluginRegistry.loadAllEnabledPlugins();
                // Notify parent component
                if (onPluginChange) {
                    onPluginChange(plugins);
                }
            }
            catch (err) {
                setError(err instanceof Error ? err.message : 'Failed to load plugins');
            }
            finally {
                setLoading(false);
            }
        };
        loadPlugins();
    }, [initialized, onPluginChange]);
    // Toggle plugin enabled/disabled
    const togglePlugin = async (id, enabled) => {
        try {
            if (enabled) {
                // Enable plugin
                plugin_registry_js_1.pluginRegistry.enablePlugin(id);
                await plugin_registry_js_1.pluginRegistry.loadPlugin(id);
            }
            else {
                // Disable plugin
                await plugin_registry_js_1.pluginRegistry.disablePlugin(id);
            }
            // Update available plugins
            const updatedPlugins = plugin_registry_js_1.pluginRegistry.getAvailablePlugins();
            setAvailablePlugins(updatedPlugins);
            // Notify parent component
            if (onPluginChange) {
                onPluginChange(updatedPlugins);
            }
        }
        catch (err) {
            setError(err instanceof Error ? err.message : `Failed to ${enabled ? 'enable' : 'disable'} plugin`);
        }
    };
    if (loading && !availablePlugins.length) {
        return ((0, jsx_runtime_1.jsx)("div", { className: "p-4 bg-white rounded-lg shadow", children: (0, jsx_runtime_1.jsxs)("div", { className: "flex items-center justify-center h-40", children: [(0, jsx_runtime_1.jsx)("div", { className: "animate-spin rounded-full h-8 w-8 border-b-2 border-blue-500" }), (0, jsx_runtime_1.jsx)("span", { className: "ml-2", children: "Loading plugins..." })] }) }));
    }
    return ((0, jsx_runtime_1.jsxs)("div", { className: "p-4 bg-white rounded-lg shadow", children: [(0, jsx_runtime_1.jsx)("h2", { className: "text-xl font-semibold mb-4", children: "Plugin Manager" }), error && ((0, jsx_runtime_1.jsxs)("div", { className: "mb-4 p-3 bg-red-100 text-red-700 rounded-md", children: [(0, jsx_runtime_1.jsx)("p", { className: "font-medium", children: "Error" }), (0, jsx_runtime_1.jsx)("p", { children: error })] })), (0, jsx_runtime_1.jsx)("div", { className: "mb-4", children: (0, jsx_runtime_1.jsxs)("p", { className: "text-gray-600", children: [availablePlugins.length, " plugins available, ", availablePlugins.filter(p => p.enabled).length, " enabled"] }) }), (0, jsx_runtime_1.jsx)("div", { className: "space-y-3", children: availablePlugins.map(plugin => ((0, jsx_runtime_1.jsxs)("div", { className: "p-3 border rounded-lg flex items-center justify-between", children: [(0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsxs)("div", { className: "flex items-center", children: [(0, jsx_runtime_1.jsx)("h3", { className: "font-medium", children: plugin.name }), (0, jsx_runtime_1.jsx)("span", { className: "ml-2 px-2 py-0.5 text-xs rounded-full bg-gray-100 text-gray-700", children: plugin.type }), (0, jsx_runtime_1.jsxs)("span", { className: "ml-2 text-xs text-gray-500", children: ["v", plugin.version] })] }), (0, jsx_runtime_1.jsx)("p", { className: "text-sm text-gray-600 mt-1", children: plugin.description }), (0, jsx_runtime_1.jsxs)("div", { className: "mt-1 text-xs text-gray-500", children: ["By ", plugin.author, plugin.repository && ((0, jsx_runtime_1.jsx)("a", { href: plugin.repository, className: "ml-2 text-blue-500 hover:text-blue-700", target: "_blank", rel: "noopener noreferrer", children: "Repository" }))] })] }), (0, jsx_runtime_1.jsx)("div", { children: (0, jsx_runtime_1.jsxs)("label", { className: "inline-flex items-center cursor-pointer", children: [(0, jsx_runtime_1.jsx)("input", { type: "checkbox", className: "sr-only peer", checked: plugin.enabled, onChange: e => togglePlugin(plugin.id, e.target.checked) }), (0, jsx_runtime_1.jsx)("div", { className: "relative w-11 h-6 bg-gray-200 rounded-full peer peer-checked:after:translate-x-full rtl:peer-checked:after:-translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:start-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-blue-600" }), (0, jsx_runtime_1.jsx)("span", { className: "ml-3 text-sm font-medium text-gray-700", children: plugin.enabled ? 'Enabled' : 'Disabled' })] }) })] }, plugin.id))) }), availablePlugins.length === 0 && !loading && ((0, jsx_runtime_1.jsx)("div", { className: "py-8 text-center text-gray-500", children: (0, jsx_runtime_1.jsx)("p", { children: "No plugins available" }) }))] }));
};
exports.PluginManager = PluginManager;
exports.default = exports.PluginManager;
//# sourceMappingURL=PluginManager.js.map