import 'dart:convert';

import 'package:userfront/core/i18n/locale_utils.dart';
import 'package:userfront/core/services/auth_proxy_service.dart';

bool shouldRouteTenantAccessErrorToErrorScreen(Object error) {
  return error is AuthProxyException && error.errorCode == 'tenant_not_allowed';
}

bool shouldRouteConsentErrorToErrorScreen(Object error) {
  return shouldRouteTenantAccessErrorToErrorScreen(error);
}

String buildTenantAccessErrorPath(Object error, Uri baseUri) {
  final authError = error as AuthProxyException;
  final localeCode =
      extractLocaleFromPath(baseUri) ?? resolvePreferredLocaleCode();
  return buildLocalizedPath(
    localeCode,
    Uri(
      path: '/error',
      queryParameters: {
        'error': authError.errorCode,
        'error_description': authError.message,
        if (authError.details != null) 'details': jsonEncode(authError.details),
      },
    ),
  );
}
