import Foundation func formatPhoneBR(_ input: String) -> String { let digits = input.filter(\.isNumber) let limited = String(digits.prefix(11)) let count = limited.count guard count > 0 else { return "" } if count <= 2 { return "(\(limited)" } let area = String(limited.prefix(2)) let remainder = String(limited.dropFirst(2)) if count <= 7 { return "(\(area)) \(remainder)" } let firstPart = String(remainder.prefix(5)) let secondPart = String(remainder.dropFirst(5)) return "(\(area)) \(firstPart)-\(secondPart)" } func normalizePhoneNumberForAPI(_ input: String) -> String { let digitsOnly = input.filter(\.isNumber) if digitsOnly.count < 10 { return "" } if digitsOnly.hasPrefix("55") { return "+\(digitsOnly)" } return "+55\(digitsOnly)" } func userFacingAuthErrorMessage(_ error: Error) -> String { if let serviceError = error as? ApiServiceError { switch serviceError { case .sessionExpired(let message): if let message, !message.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { return message } return "Sua sessão expirou. Faça login novamente." } } if let networkError = error as? NetworkError { switch networkError { case .unauthorized(let message): if let message, !message.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { return message } return "Seu acesso expirou. Solicite um novo código para continuar." case .httpError(let code, let message): if let message, !message.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { return message } switch code { case 400: return "Não foi possível validar seus dados. Revise as informações e tente novamente." case 401, 403: return "Seu acesso expirou. Solicite um novo código para continuar." case 404: return "Não encontramos seu cadastro com os dados informados." case 429: return "Muitas tentativas em sequência. Aguarde um instante e tente novamente." case 500...599: return "Nossos servidores estão instáveis no momento. Tente novamente em alguns minutos." default: return "Não foi possível concluir a operação agora. Tente novamente." } case .rateLimited: return "Muitas tentativas em sequência. Aguarde um instante e tente novamente." case .transportError: return "Não foi possível se conectar ao servidor. Tente novamente." case .invalidURL, .invalidResponse, .decodeError: return "Ocorreu uma instabilidade ao processar sua solicitação. Tente novamente." case .cancelled: return "Cancelado" } } return "Não foi possível concluir a operação. Tente novamente." }