import type { MobileAppLifecycleAdapter } from '../../contracts/mobile.contracts';

/** Visibility API fallback for browser runtimes. */
export class WebMobileAppLifecycleAdapter implements MobileAppLifecycleAdapter {
  private handler: ((isActive: boolean) => void) | null = null;

  async initialize(): Promise<void> {
    if (typeof document === 'undefined') {
      return;
    }

    document.addEventListener('visibilitychange', () => {
      this.handler?.(!document.hidden);
    });
  }

  onAppStateChange(handler: (isActive: boolean) => void): void {
    this.handler = handler;
  }
}
