export class NetworkDetector {
  constructor() {
    this.isOnline = navigator.onLine;
    this.listeners = [];
  }

  subscribe(listener) {
    this.listeners.push(listener);

    window.addEventListener('online', () => {
      this.isOnline = true;
      this.listeners.forEach(l => l(true));
    });

    window.addEventListener('offline', () => {
      this.isOnline = false;
      this.listeners.forEach(l => l(false));
    });

    return () => {
      this.listeners = this.listeners.filter(l => l !== listener);
    };
  }

  getStatus() {
    return this.isOnline;
  }
}

export const networkDetector = new NetworkDetector();
