// Core Config Management - Handles loading, caching, and basic operations
class ConfigCore {
  constructor() {
    this.cache = new Map();
  }

  getRandomLoadingMessage() {
    const messages = [
      "Preparing your configuration settings...",
      "Gathering finest configuration options...",
      "Loading configuration data...",
      "Fetching your preferences...",
      "Setting up your workspace...",
      "Loading configuration components...",
      "Preparing configuration interface...",
      "Gathering system settings...",
      "Loading configuration modules...",
      "Initializing configuration system..."
    ];
    return messages[Math.floor(Math.random() * messages.length)];
  }

  async loadConfig(category) {
    //console.log(`ConfigCore: Loading ${category} config...`);
    
    if (this.cache.has('unified')) {
      //console.log(`ConfigCore: Using cached unified config`);
      const cachedData = this.cache.get('unified');
      window.configData = cachedData; // Make available globally
      return cachedData.subcategories || {};
    }

    try {
      //console.log(`ConfigCore: Fetching from /data/config/generated/configs.json`);
      const response = await fetch('/data/config/generated/configs.json');
      //console.log(`ConfigCore: Response status: ${response.status}`);
      
      if (!response.ok) {
        throw new Error(`HTTP ${response.status}: ${response.statusText}`);
      }
      
      const configData = await response.json();
      //console.log(`ConfigCore: Loaded config data:`, configData);
      
      this.cache.set('unified', configData);
      window.configData = configData; // Make available globally
      
      // Return the actual subcategories, not just the category metadata
      const categoryData = configData.subcategories || {};
      //console.log(`ConfigCore: Available subcategories:`, Object.keys(categoryData));
      
      return categoryData;
    } catch (error) {
      console.error(`ConfigCore: Error loading ${category} config:`, error);
      return {};
    }
  }

  filterConfigByCategory(unifiedData, category) {
    if (!unifiedData || !unifiedData.config) {
      return {};
    }
    
    const categoryData = {};
    Object.entries(unifiedData.config).forEach(([key, value]) => {
      if (value.category === category) {
        categoryData[key] = value;
      }
    });
    
    return categoryData;
  }

  clearCache() {
    this.cache.clear();
  }
}

// Export for use in other modules
window.ConfigCore = ConfigCore;
