part of '../../core_app.dart';

const Duration _releaseStartupWatchdogDuration = Duration(seconds: 15);
const Duration _debugStartupWatchdogDuration = Duration(seconds: 15);

// ── 错误提示映射 ──

String _friendlyErrorTitle(WebResourceError error) {
  return switch (error.errorType) {
    WebResourceErrorType.timeout => '请求超时',
    WebResourceErrorType.hostLookup ||
    WebResourceErrorType.connect ||
    WebResourceErrorType.io => '网络连接失败',
    WebResourceErrorType.failedSslHandshake => '安全连接失败',
    _ => '页面加载失败',
  };
}

String _friendlyErrorMessage(WebResourceError error) {
  return switch (error.errorType) {
    WebResourceErrorType.timeout => '当前网络较慢，请稍后重新加载。',
    WebResourceErrorType.hostLookup ||
    WebResourceErrorType.connect ||
    WebResourceErrorType.io => '没有成功连接到服务器，请检查网络后重试。',
    WebResourceErrorType.failedSslHandshake => '当前站点证书校验失败，请稍后再试。',
    _ => _parseRawErrorDescription(error.description),
  };
}

String _parseRawErrorDescription(String description) {
  final cleanDesc = description.trim();
  if (cleanDesc.isEmpty) {
    return '请稍后重新加载页面。';
  }
  final lower = cleanDesc.toLowerCase();
  if (lower.contains('err_internet_disconnected') || lower.contains('err_address_unreachable') || lower.contains('err_name_not_resolved')) {
    return '没有成功连接到服务器，请检查网络后重试。';
  }
  if (lower.contains('err_connection_timed_out') || lower.contains('err_timed_out')) {
    return '当前网络较慢，请稍后重新加载。';
  }
  if (lower.contains('err_cert_') || lower.contains('err_ssl_')) {
    return '当前站点证书校验失败，请稍后再试。';
  }
  return cleanDesc;
}
