// MusicRoom - Core module
// Shared state and namespace setup

window.MusicRoom = {
  // Audio element
  audio: new Audio(),
  
  // WebSocket and channel state
  ws: null,
  currentChannelId: null,
  currentTrackId: null,
  currentTitle: null,
  serverTimestamp: 0,
  serverTrackDuration: 0,
  lastServerUpdate: 0,
  serverPaused: true,
  
  // Channels list
  channels: [],
  
  // Sync state
  wantSync: true,   // User intent - do they want to be synced?
  synced: false,    // Actual state - are we currently synced?
  
  // Volume
  preMuteVolume: 1,
  STORAGE_KEY: "musicroom_volume",
  
  // Playback state
  localTimestamp: 0,
  queue: [],
  currentIndex: 0,
  playbackMode: "repeat-all",
  
  // User state
  currentUser: null,
  serverStatus: null,
  
  // Library (all discovered tracks)
  library: [],
  
  // Caching state
  prefetchController: null,
  loadingSegments: new Set(),
  trackCaches: new Map(),      // Map of filename -> Set of cached segment indices
  trackBlobs: new Map(),       // Map of filename -> Blob URL for fully cached tracks
  bulkDownloadStarted: new Map(),
  cachedTracks: new Set(),     // Set of track IDs that are fully cached locally
  
  // Download metrics
  audioBytesPerSecond: 20000,  // Audio bitrate estimate for range requests
  downloadSpeed: 0,            // Actual network download speed
  recentDownloads: [],         // Track recent downloads for speed calculation
  
  // Constants
  SEGMENTS: 20,
  FAST_THRESHOLD: 10 * 1024 * 1024, // 10 MB/s
  
  // UI update tracking (to avoid unnecessary DOM updates)
  lastProgressPct: -1,
  lastTimeCurrent: "",
  lastTimeTotal: "",
  lastBufferPct: -1,
  lastSpeedText: ""
};
