import { validate as validateEmail } from "deep-email-validator";
import { UserProfiles } from "../models/index.js";
import { fetchMeta } from "../misc/fetch-meta.js";
export async function validateEmailForAccount(emailAddress) {
    const meta = await fetchMeta();
    const exist = await UserProfiles.countBy({
        emailVerified: true,
        email: emailAddress
    });
    const validated = meta.enableActiveEmailValidation ? await validateEmail({
        email: emailAddress,
        validateRegex: true,
        validateMx: true,
        validateTypo: false,
        validateDisposable: true,
        validateSMTP: false
    }) : {
        valid: true
    };
    const available = exist === 0 && validated.valid;
    return {
        available,
        reason: available ? null : exist !== 0 ? "used" : validated.reason === "regex" ? "format" : validated.reason === "disposable" ? "disposable" : validated.reason === "mx" ? "mx" : validated.reason === "smtp" ? "smtp" : null
    };
}
