import { PluginService } from "../services/index.js";
const pluginCache = /* @__PURE__ */ new Map();
function discoverPlugins() {
  try {
    console.log("[Plugins] Loading plugins from registry...");
    const plugins = PluginService.getAll();
    const validatedPlugins = [];
    for (const plugin of plugins) {
      const validation = validatePlugin(plugin);
      if (validation.valid) {
        validatedPlugins.push(plugin);
        pluginCache.set(plugin.name, plugin);
      } else {
        console.warn(`[Plugins] Plugin ${plugin.name} failed validation:`, validation.errors);
      }
    }
    console.log(`[Plugins] Loaded ${validatedPlugins.length} valid plugins from registry`);
    return validatedPlugins;
  } catch (error) {
    console.error("[Plugins] Error loading plugins:", error);
    return [];
  }
}
function loadPluginSafely(name) {
  const cached = pluginCache.get(name);
  if (cached) {
    console.log(`[Plugins] Using cached plugin: ${name}`);
    return cached;
  }
  const plugin = loadPluginWithValidation(name);
  if (plugin) {
    pluginCache.set(name, plugin);
  }
  return plugin;
}
function loadPluginWithValidation(name) {
  console.log(`[Plugins] Loading plugin from registry: ${name}`);
  try {
    const plugin = PluginService.get(name);
    if (!plugin) {
      console.warn(`[Plugins] Plugin not found in registry: ${name}`);
      return null;
    }
    const validation = validatePlugin(plugin);
    if (!validation.valid) {
      console.error(`[Plugins] Plugin ${name} validation failed:`, validation.errors);
      return null;
    }
    const securityCheck = performSecurityCheck(plugin);
    if (!securityCheck.safe) {
      console.error(`[Plugins] Plugin ${name} failed security check:`, securityCheck.issues);
      return null;
    }
    console.log(`[Plugins] Successfully loaded plugin from registry: ${name}`);
    return plugin;
  } catch (error) {
    console.error(`[Plugins] Error loading plugin ${name}:`, error);
    return null;
  }
}
function validatePlugin(plugin) {
  const errors = [];
  if (!plugin.name) {
    errors.push("Plugin name is required");
  }
  if (!plugin.displayName) {
    errors.push("Plugin display name is required");
  }
  if (!plugin.version) {
    errors.push("Plugin version is required");
  }
  if (plugin.name && !/^[a-z][a-z0-9-]*$/.test(plugin.name)) {
    errors.push("Plugin name must be lowercase, start with a letter, and contain only letters, numbers, and hyphens");
  }
  if (plugin.version && !/^\d+\.\d+\.\d+$/.test(plugin.version)) {
    errors.push("Plugin version must follow semver format (x.y.z)");
  }
  if (plugin.dependencies) {
    for (const dep of plugin.dependencies) {
      if (!dep || typeof dep !== "string") {
        errors.push(`Invalid dependency: ${dep}`);
      }
    }
  }
  return {
    valid: errors.length === 0,
    errors
  };
}
function performSecurityCheck(plugin) {
  const issues = [];
  if (plugin.name.includes("..") || plugin.name.includes("/") || plugin.name.includes("\\")) {
    issues.push("Plugin name contains suspicious path characters");
  }
  return {
    safe: issues.length === 0,
    issues
  };
}
async function registerPlugin(plugin) {
  var _a;
  try {
    console.log(`[Plugins] Registering plugin: ${plugin.name}`);
    if ((_a = plugin.hooks) == null ? void 0 : _a.onLoad) {
      await plugin.hooks.onLoad();
    }
    if (plugin.hooks) {
      await registerPluginHooks(plugin.name, plugin.hooks);
    }
    console.log(`[Plugins] Successfully registered plugin: ${plugin.name}`);
    return true;
  } catch (error) {
    console.error(`[Plugins] Error registering plugin ${plugin.name}:`, error);
    return false;
  }
}
async function registerPluginHooks(pluginName, hooks) {
  console.log(`[Plugins] Registered hooks for plugin: ${pluginName}`, Object.keys(hooks));
}
async function unloadPlugin(name) {
  var _a;
  try {
    const plugin = pluginCache.get(name);
    if (!plugin) {
      console.warn(`[Plugins] Plugin not loaded: ${name}`);
      return true;
    }
    if ((_a = plugin.hooks) == null ? void 0 : _a.onUnload) {
      await plugin.hooks.onUnload();
    }
    pluginCache.delete(name);
    console.log(`[Plugins] Successfully unloaded plugin: ${name}`);
    return true;
  } catch (error) {
    console.error(`[Plugins] Error unloading plugin ${name}:`, error);
    return false;
  }
}
function getPluginStats() {
  return {
    loaded: pluginCache.size,
    cached: pluginCache.size,
    loading: 0
    // Registry-based loading is instant, no async operations
  };
}
function clearPluginCache() {
  pluginCache.clear();
  console.log("[Plugins] Cleared plugin cache");
}
export {
  clearPluginCache,
  discoverPlugins,
  getPluginStats,
  loadPluginSafely,
  registerPlugin,
  unloadPlugin,
  validatePlugin
};
