/**
 * US-only phone helpers for the check-in form. The API's PhoneNormalizer is the
 * source of truth; this is just enough client-side formatting + validation to
 * give fast feedback and submit clean digits (D-edge: non-US numbers rejected
 * gracefully).
 */

/** Strip to digits and drop a leading US country code. */
export function normalizeUsPhone(input: string): string {
  const digits = input.replace(/\D/g, "");
  if (digits.length === 11 && digits.startsWith("1")) return digits.slice(1);
  return digits;
}

/** A valid US number is exactly 10 digits after normalization. */
export function isValidUsPhone(input: string): boolean {
  return normalizeUsPhone(input).length === 10;
}

/** Format for display, e.g. "(512) 555-1234"; partial input formats as typed. */
export function formatUsPhone(input: string): string {
  const d = normalizeUsPhone(input).slice(0, 10);
  if (d.length <= 3) return d;
  if (d.length <= 6) return `(${d.slice(0, 3)}) ${d.slice(3)}`;
  return `(${d.slice(0, 3)}) ${d.slice(3, 6)}-${d.slice(6)}`;
}
