createSecureRedirectUrl function

String createSecureRedirectUrl(
  1. String fi,
  2. String reqdate,
  3. String ecreq, {
  4. String? theme,
})

Creates a secure URL for the WebView with the token parameters

Implementation

String createSecureRedirectUrl(String fi, String reqdate, String ecreq, {String? theme}) {
  // Use the current environment's URL
  final String baseUrl = SaafeSdk.baseRedirectUrl;
  final Uri url = Uri.parse(baseUrl);

  // IMPORTANT: Start with existing query parameters from base URL
  // This preserves custom parameters like theme JSON that might be in the base URL
  final Map<String, String> queryParams = Map<String, String>.from(url.queryParameters);

  // Override/add the required query parameters
  if (fi.isNotEmpty) queryParams['fi'] = fi;
  if (reqdate.isNotEmpty) queryParams['reqdate'] = reqdate;
  if (ecreq.isNotEmpty) queryParams['ecreq'] = ecreq;

  // Add platform flag (override if exists)
  if (Platform.isIOS) {
    queryParams['platform'] = 'ios';
  } else if (Platform.isAndroid) {
    queryParams['platform'] = 'android';
  } else {
    queryParams['platform'] = 'flutter';
  }

  // Add theme flag ONLY if:
  // 1. Theme is explicitly provided AND it's 'light' or 'dark' (not custom JSON)
  // 2. OR if there's no existing theme parameter in the base URL
  // This preserves custom theme JSON from the base URL
  final bool hasCustomTheme = url.queryParameters.containsKey('theme') &&
                               url.queryParameters['theme'] != null &&
                               url.queryParameters['theme']!.startsWith('{');

  if (hasCustomTheme) {
    // Preserve the custom theme JSON from base URL - don't override it
    debugPrint("SAAFE SDK: Preserving custom theme JSON from base URL");
  } else {
    // Only add SDK's theme if no custom theme exists
    if (theme != null && (theme == 'light' || theme == 'dark')) {
      queryParams['theme'] = theme;
    } else {
      // Get system theme
      final Brightness systemBrightness = WidgetsBinding.instance.platformDispatcher.platformBrightness;
      queryParams['theme'] = systemBrightness == Brightness.dark ? 'dark' : 'light';
    }
  }

  // Log if parameters are empty
  if (queryParams.isEmpty || (fi.isEmpty || reqdate.isEmpty || ecreq.isEmpty)) {
    debugPrint("SAAFE SDK Warning: Some or all parameters are empty. This will result in an incomplete URL.");
  }

  final String finalUrl = url.replace(queryParameters: queryParams).toString();
  debugPrint("SAAFE SDK: Final URL constructed: $finalUrl");
  return finalUrl;
}