// PWA service for base module
export class PWAService {
  private deferredPrompt: any = null
  
  async initialize(): Promise<void> {
    console.log('📱 Initializing PWA service...')
    
    // Listen for beforeinstallprompt event
    window.addEventListener('beforeinstallprompt', (e) => {
      console.log('PWA install prompt available')
      e.preventDefault()
      this.deferredPrompt = e
    })
    
    // Listen for app installed event
    window.addEventListener('appinstalled', () => {
      console.log('PWA was installed')
      this.deferredPrompt = null
    })
  }
  
  canInstall(): boolean {
    return this.deferredPrompt !== null
  }
  
  async install(): Promise<boolean> {
    if (!this.deferredPrompt) {
      return false
    }
    
    this.deferredPrompt.prompt()
    const result = await this.deferredPrompt.userChoice
    
    if (result.outcome === 'accepted') {
      console.log('User accepted PWA install')
    } else {
      console.log('User dismissed PWA install')
    }
    
    this.deferredPrompt = null
    return result.outcome === 'accepted'
  }
}

export const pwaService = new PWAService()