import 'dart:html' as html;
import 'dart:async';

void implSendLoginSuccess(String token) {
  final message = {'type': 'LOGIN_SUCCESS', 'token': token};
  
  if (html.window.opener != null) {
    try {
      html.window.opener!.postMessage(message, '*');
      print("Sent login success message to opener");
    } catch (e) {
      print("Failed to postMessage: $e");
    }
    
    // Close the popup after a short delay to ensure message sending
    Timer(const Duration(milliseconds: 500), () {
      html.window.close();
    });
  } else {
     // Should not happen given isPopup check, but as fallback:
     print("No opener found during popup flow.");
  }
}

bool implIsPopup() {
  return html.window.opener != null;
}
