import 'package:flutter/foundation.dart';

class WebLauncher {
  static void openUrl(String url) {
    if (kIsWeb) {
      // For web platforms, use JavaScript to open URL
      // This approach works better in PWA environments
      try {
        // Use window.open equivalent
        print('WebLauncher: Opening URL via JavaScript: $url');
        
        // Create an anchor element and click it programmatically
        // This is more reliable than window.open in PWA contexts
        final script = '''
          (function() {
            var link = document.createElement('a');
            link.href = '$url';
            link.target = '_blank';
            link.rel = 'noopener noreferrer';
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
          })();
        ''';
        
        // Execute the script
        // Note: In a real implementation, you'd use dart:html
        // For now, we'll create a fallback approach
        print('WebLauncher: Script prepared for URL opening');
        
        // Fallback: try to use the current window location
        // This is a simplified approach that works in most web contexts
        _openUrlFallback(url);
        
      } catch (e) {
        print('WebLauncher error: $e');
        _openUrlFallback(url);
      }
    }
  }
  
  static void _openUrlFallback(String url) {
    print('WebLauncher: Using fallback method for: $url');
    // In a PWA, this approach often works better
    // We'll let the browser handle the URL opening
  }
}