const VN_PHONE_REGEX = /^(?:\+84|84|0)(3[2-9]|5[2689]|7[06-9]|8[1-9]|9[0-9])\d{7}$/;

export function isValidVietnamPhone(phone: string): boolean {
  const cleaned = phone.replace(/[\s.-]/g, '');
  return VN_PHONE_REGEX.test(cleaned);
}

export function normalizeVietnamPhone(phone: string): string | null {
  const cleaned = phone.replace(/[\s.-]/g, '');
  if (!VN_PHONE_REGEX.test(cleaned)) return null;

  if (cleaned.startsWith('+84')) return cleaned;
  if (cleaned.startsWith('84')) return `+${cleaned}`;
  if (cleaned.startsWith('0')) return `+84${cleaned.slice(1)}`;
  return null;
}
