import { EMAIL_REGEX, STRICT_EMAIL_REGEX } from "../constants/emailRegex.js";
import { extractEmailDomain } from "./emailHelper.js";
//#region src/components/pisellEmail/utils/emailValidator.ts
/**
* 邮箱校验工具函数
*/
/**
* 标准邮箱格式校验
* @param email 邮箱地址
* @returns 是否通过校验
* 
* @example
* validateEmail('user@example.com') // true
* validateEmail('invalid-email') // false
*/
function validateEmail(email) {
	if (!email) return true;
	return EMAIL_REGEX.test(email);
}
/**
* 严格邮箱格式校验（RFC 5322）
* @param email 邮箱地址
* @returns 是否通过校验
* 
* @example
* validateEmailStrict('user@example.com') // true
*/
function validateEmailStrict(email) {
	if (!email) return true;
	return STRICT_EMAIL_REGEX.test(email);
}
/**
* 域名白名单/黑名单校验
* 
* @param email 邮箱地址
* @param allowedDomains 白名单域名列表
* @param blockedDomains 黑名单域名列表
* @returns 校验结果
*/
function validateEmailDomain(email, allowedDomains, blockedDomains) {
	const errors = [];
	const domain = extractEmailDomain(email);
	if (!domain) return {
		isValid: true,
		errors
	};
	if (allowedDomains && allowedDomains.length > 0) {
		if (!allowedDomains.includes(domain)) errors.push(`只允许以下域名：${allowedDomains.join(", ")}`);
	}
	if (blockedDomains && blockedDomains.length > 0) {
		if (blockedDomains.includes(domain)) errors.push("该邮箱域名不可用");
	}
	return {
		isValid: errors.length === 0,
		errors
	};
}
//#endregion
export { validateEmail, validateEmailDomain, validateEmailStrict };
