/**
 * i18n PR2 - phone helpers.
 *
 * Validates:
 *  1. parsePhone yields E.164 + country + display formats
 *  2. parsePhone returns the empty record for unparseable input
 *  3. AsYouType formats digits in the country's national style
 *  4. isValidE164 accepts only E.164 form
 *  5. callingCodeFor returns the dial prefix
 *  6. International-format paste detects country
 */

import { describe, it, expect } from 'vitest';
import { parsePhone, formatAsYouType, isValidE164, callingCodeFor } from '@/lib/i18n/phone';

describe('i18n phone', () => {
  it('parses an international-format input regardless of defaultCountry', () => {
    const r = parsePhone('+44 20 7946 0958');
    expect(r.e164).toBe('+442079460958');
    expect(r.country).toBe('GB');
    expect(r.isValid).toBe(true);
    expect(r.national).toMatch(/020 7946 0958/);
    expect(r.international).toMatch(/\+44 20 7946 0958/);
  });

  it('parses a national-format input against the defaultCountry', () => {
    const r = parsePhone('020 7946 0958', 'GB');
    expect(r.e164).toBe('+442079460958');
    expect(r.country).toBe('GB');
    expect(r.isValid).toBe(true);
  });

  it('returns the empty record for empty / nonsense input', () => {
    expect(parsePhone('').isValid).toBe(false);
    expect(parsePhone('abc').isValid).toBe(false);
    expect(parsePhone('').e164).toBeNull();
  });

  it('formatAsYouType produces national-format output', () => {
    // GB national-format breaks 020 7946 0958.
    const out = formatAsYouType('2079460958', 'GB');
    expect(out.length).toBeGreaterThan(0);
    // US national-format breaks (415) 555-1234.
    const us = formatAsYouType('4155551234', 'US');
    expect(us).toContain('415');
  });

  it('isValidE164 accepts only E.164', () => {
    expect(isValidE164('+442079460958')).toBe(true);
    expect(isValidE164('+1 415 555 1234')).toBe(true); // libphonenumber tolerates spaces
    expect(isValidE164('020 7946 0958')).toBe(false); // missing +country prefix
    expect(isValidE164('not a phone')).toBe(false);
  });

  it('callingCodeFor returns the dial prefix', () => {
    expect(callingCodeFor('US')).toBe('+1');
    expect(callingCodeFor('GB')).toBe('+44');
    expect(callingCodeFor('PL')).toBe('+48');
  });
});
