import Foundation

enum EmailText {
    nonisolated static func normalizedForStorage(_ value: String) -> String {
        value
            .trimmingCharacters(in: .whitespacesAndNewlines)
            .lowercased()
    }

    nonisolated static func isValid(_ value: String) -> Bool {
        let trimmed = normalizedForStorage(value)
        guard !trimmed.isEmpty, !trimmed.contains(" ") else { return false }

        let parts = trimmed.split(separator: "@", omittingEmptySubsequences: false)
        guard parts.count == 2 else { return false }

        let local = String(parts[0])
        let domain = String(parts[1])

        guard !local.isEmpty, !domain.isEmpty else { return false }
        guard domain.contains("."), !domain.hasPrefix("."), !domain.hasSuffix(".") else { return false }
        return true
    }
}
