import email_validator
from email_validator import EmailNotValidError, ValidatedEmail

from service.core.exceptions import InvalidEmail


def validate_email(value: str, use_dns: bool = None) -> ValidatedEmail:
    try:
        return email_validator.validate_email(
            value,
            # globally_deliverable=use_dns,
            check_deliverability=use_dns,
            allow_domain_literal=False,
        )
    except EmailNotValidError as e:
        raise InvalidEmail(str(e))
