import { config } from "dotenv";
import { join } from "path";
import { readdirSync, existsSync, statSync } from "fs";
class PluginEnvLoader {
  static instance = null;
  pluginEnvs;
  loaded = false;
  constructor() {
    this.pluginEnvs = /* @__PURE__ */ new Map();
  }
  /**
   * Get singleton instance
   */
  static getInstance() {
    if (!PluginEnvLoader.instance) {
      PluginEnvLoader.instance = new PluginEnvLoader();
    }
    return PluginEnvLoader.instance;
  }
  /**
   * Load all plugin .env files
   */
  loadAllPlugins() {
    if (this.loaded) return;
    try {
      const pluginsDir = join(process.cwd(), "contents/plugins");
      if (!existsSync(pluginsDir)) {
        console.warn("[Plugin Env Loader] No plugins directory found");
        this.loaded = true;
        return;
      }
      const entries = readdirSync(pluginsDir);
      for (const entry of entries) {
        const pluginPath = join(pluginsDir, entry);
        if (!statSync(pluginPath).isDirectory()) continue;
        const envPath = join(pluginPath, ".env");
        let pluginEnv;
        if (existsSync(envPath)) {
          const result = config({ path: envPath, override: false });
          if (result.error) {
            console.error(`[Plugin Env Loader] Error loading ${entry}/.env:`, result.error);
            pluginEnv = { ...process.env };
          } else if (result.parsed) {
            pluginEnv = { ...process.env, ...result.parsed };
            if (process.env.NODE_ENV === "development") {
              console.log(`[Plugin Env Loader] \u2713 Loaded ${entry}/.env (plugin-specific configuration)`);
            }
          } else {
            pluginEnv = { ...process.env };
          }
        } else {
          pluginEnv = { ...process.env };
          if (process.env.NODE_ENV === "development") {
            console.log(`[Plugin Env Loader] \u2139\uFE0F  ${entry}: Using process.env (no plugin .env file found)`);
          }
        }
        this.pluginEnvs.set(entry, pluginEnv);
      }
      this.loaded = true;
    } catch (error) {
      console.error("[Plugin Env Loader] Failed to load plugin environments:", error);
      this.loaded = true;
    }
  }
  /**
   * Get environment configuration for a specific plugin
   */
  getPluginEnv(pluginName) {
    if (!this.loaded) {
      this.loadAllPlugins();
    }
    const pluginEnv = this.pluginEnvs.get(pluginName);
    if (!pluginEnv) {
      if (process.env.NODE_ENV === "development") {
        console.warn(`[Plugin Env Loader] No .env found for plugin: ${pluginName}`);
      }
      return {};
    }
    return pluginEnv;
  }
  /**
   * Check if a plugin has a .env file loaded
   */
  hasPluginEnv(pluginName) {
    if (!this.loaded) {
      this.loadAllPlugins();
    }
    return this.pluginEnvs.has(pluginName);
  }
  /**
   * Get list of all loaded plugins
   */
  getLoadedPlugins() {
    if (!this.loaded) {
      this.loadAllPlugins();
    }
    return Array.from(this.pluginEnvs.keys());
  }
  /**
   * Force reload all plugin environments
   */
  reload() {
    this.loaded = false;
    this.pluginEnvs.clear();
    this.loadAllPlugins();
  }
}
function getPluginEnv(pluginName) {
  return PluginEnvLoader.getInstance().getPluginEnv(pluginName);
}
function hasPluginEnv(pluginName) {
  return PluginEnvLoader.getInstance().hasPluginEnv(pluginName);
}
function getLoadedPlugins() {
  return PluginEnvLoader.getInstance().getLoadedPlugins();
}
function reloadPluginEnvs() {
  PluginEnvLoader.getInstance().reload();
}
function initializePluginEnvs() {
  PluginEnvLoader.getInstance().getLoadedPlugins();
}
export {
  getLoadedPlugins,
  getPluginEnv,
  hasPluginEnv,
  initializePluginEnvs,
  reloadPluginEnvs
};
