function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
/**
 * 邮箱校验工具函数
 */

import { EMAIL_REGEX, STRICT_EMAIL_REGEX } from "../constants/emailRegex";
import { extractEmailDomain } from "./emailHelper";

/**
 * 标准邮箱格式校验
 * @param email 邮箱地址
 * @returns 是否通过校验
 * 
 * @example
 * validateEmail('user@example.com') // true
 * validateEmail('invalid-email') // false
 */
export function validateEmail(email) {
  if (!email) return true; // 空值不校验，由 required 处理
  return EMAIL_REGEX.test(email);
}

/**
 * 严格邮箱格式校验（RFC 5322）
 * @param email 邮箱地址
 * @returns 是否通过校验
 * 
 * @example
 * validateEmailStrict('user@example.com') // true
 */
export function validateEmailStrict(email) {
  if (!email) return true;
  return STRICT_EMAIL_REGEX.test(email);
}

/**
 * 邮箱分部校验
 * 检查邮箱的各个部分是否符合规范
 * 
 * @param email 邮箱地址
 * @returns 校验结果
 */
export function validateEmailParts(email) {
  var errors = [];

  // 1. 基本格式检查
  if (!email.includes('@')) {
    errors.push('邮箱地址必须包含 @ 符号');
    return {
      isValid: false,
      errors: errors
    };
  }
  var _email$split = email.split('@'),
    _email$split2 = _slicedToArray(_email$split, 2),
    localPart = _email$split2[0],
    domainPart = _email$split2[1];

  // 2. 本地部分校验
  if (!localPart || localPart.length === 0) {
    errors.push('@ 符号前不能为空');
  }
  if (localPart.length > 64) {
    errors.push('@ 符号前最多 64 个字符');
  }

  // 3. 域名部分校验
  if (!domainPart || domainPart.length === 0) {
    errors.push('@ 符号后不能为空');
  }
  if (!domainPart.includes('.')) {
    errors.push('域名必须包含 . 符号');
  }
  if (domainPart.endsWith('.')) {
    errors.push('域名不能以 . 结尾');
  }

  // 4. 顶级域名校验
  var parts = domainPart.split('.');
  var tld = parts[parts.length - 1];
  if (tld && tld.length < 2) {
    errors.push('顶级域名至少 2 个字符');
  }
  return {
    isValid: errors.length === 0,
    errors: errors
  };
}

/**
 * 域名白名单/黑名单校验
 * 
 * @param email 邮箱地址
 * @param allowedDomains 白名单域名列表
 * @param blockedDomains 黑名单域名列表
 * @returns 校验结果
 */
export function validateEmailDomain(email, allowedDomains, blockedDomains) {
  var errors = [];
  var domain = extractEmailDomain(email);
  if (!domain) {
    return {
      isValid: true,
      errors: errors
    };
  }

  // 白名单校验
  if (allowedDomains && allowedDomains.length > 0) {
    if (!allowedDomains.includes(domain)) {
      errors.push("\u53EA\u5141\u8BB8\u4EE5\u4E0B\u57DF\u540D\uFF1A".concat(allowedDomains.join(', ')));
    }
  }

  // 黑名单校验
  if (blockedDomains && blockedDomains.length > 0) {
    if (blockedDomains.includes(domain)) {
      errors.push('该邮箱域名不可用');
    }
  }
  return {
    isValid: errors.length === 0,
    errors: errors
  };
}