"use client";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { createContext, useContext, useEffect, useState } from "react";
const PluginContext = createContext(null);
function PluginProvider({ children }) {
  const [plugins, setPlugins] = useState([]);
  const [isLoading, setIsLoading] = useState(true);
  const [error, setError] = useState(null);
  useEffect(() => {
    let mounted = true;
    const initializePlugins = async () => {
      try {
        console.log("[PluginProvider] Initializing plugin system...");
        if (!mounted) return;
        const activePlugins = [];
        setPlugins(activePlugins);
        setError(null);
        console.log(
          `[PluginProvider] Loaded ${activePlugins.length} active plugins:`,
          activePlugins.map((p) => p.name)
        );
      } catch (err) {
        console.error("[PluginProvider] Error initializing plugins:", err);
        if (mounted) {
          setError(err instanceof Error ? err.message : "Failed to initialize plugins");
        }
      } finally {
        if (mounted) {
          setIsLoading(false);
        }
      }
    };
    initializePlugins();
    return () => {
      mounted = false;
    };
  }, []);
  const isPluginLoaded = (name) => {
    return plugins.some((p) => p.name === name);
  };
  const getPlugin = (name) => {
    const plugin = plugins.find((p) => p.name === name);
    return plugin || null;
  };
  const getPluginComponent = (pluginName, componentName) => {
    var _a;
    const plugin = getPlugin(pluginName);
    if (!((_a = plugin == null ? void 0 : plugin.components) == null ? void 0 : _a[componentName])) {
      console.warn(`[PluginProvider] Component ${componentName} not found in plugin ${pluginName}`);
      return null;
    }
    return plugin.components[componentName];
  };
  const getPluginService = (pluginName, serviceName) => {
    var _a;
    const plugin = getPlugin(pluginName);
    if (!((_a = plugin == null ? void 0 : plugin.services) == null ? void 0 : _a[serviceName])) {
      console.warn(`[PluginProvider] Service ${serviceName} not found in plugin ${pluginName}`);
      return null;
    }
    return plugin.services[serviceName];
  };
  const contextValue = {
    plugins,
    isLoading,
    error,
    isPluginLoaded,
    getPlugin,
    getPluginComponent,
    getPluginService
  };
  if (isLoading) {
    return /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center min-h-screen", children: /* @__PURE__ */ jsxs("div", { className: "text-center space-y-4", children: [
      /* @__PURE__ */ jsx("div", { className: "animate-spin rounded-full h-8 w-8 border-b-2 border-primary mx-auto" }),
      /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: "Loading plugins..." })
    ] }) });
  }
  if (error) {
    console.error("[PluginProvider] Plugin system error:", error);
  }
  return /* @__PURE__ */ jsx(PluginContext.Provider, { value: contextValue, children });
}
function usePlugins() {
  const context = useContext(PluginContext);
  if (!context) {
    throw new Error("usePlugins must be used within a PluginProvider");
  }
  return context;
}
function usePlugin(name) {
  const { getPlugin } = usePlugins();
  return getPlugin(name);
}
function usePluginComponent(pluginName, componentName) {
  const { getPluginComponent } = usePlugins();
  return getPluginComponent(pluginName, componentName);
}
function usePluginService(pluginName, serviceName) {
  const { getPluginService } = usePlugins();
  return getPluginService(pluginName, serviceName);
}
function withPluginComponent(pluginName, componentName, fallback) {
  return function WithPluginComponent(props) {
    const Component = usePluginComponent(pluginName, componentName);
    if (!Component) {
      if (fallback) {
        const FallbackComponent = fallback;
        return /* @__PURE__ */ jsx(FallbackComponent, { ...props });
      }
      return null;
    }
    return /* @__PURE__ */ jsx(Component, { ...props });
  };
}
function PluginGate({ pluginName, children, fallback }) {
  const { isPluginLoaded } = usePlugins();
  if (isPluginLoaded(pluginName)) {
    return /* @__PURE__ */ jsx(Fragment, { children });
  }
  return /* @__PURE__ */ jsx(Fragment, { children: fallback });
}
export {
  PluginGate,
  PluginProvider,
  usePlugin,
  usePluginComponent,
  usePluginService,
  usePlugins,
  withPluginComponent
};
