// 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) {
    
    if (this.cache.has('unified')) {
      const cachedData = this.cache.get('unified');
      window.configData = cachedData; // Make available globally
      return cachedData.subcategories || {};
    }

    try {
      const response = await fetch('/data/config/generated/configs.json');
      
      if (!response.ok) {
        throw new Error(`HTTP ${response.status}: ${response.statusText}`);
      }
      
      const configData = await response.json();
      
      this.cache.set('unified', configData);
      window.configData = configData; // Make available globally
      
      // Return the actual subcategories, not just the category metadata
      const categoryData = configData.subcategories || {};
      
      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;
