import {
  getDialCode,
  getCountryInitialValue,
  getPhoneInitialValue,
  getPhoneMaxLength,
} from './utils';

// Mock the phone utilities
jest.mock('@shared/utils/phone', () => {
  const actual = jest.requireActual('@shared/utils/phone');
  return {
    ...actual,
    getFormattedPhoneNumber: jest.fn((dialCode, phoneNumber) => {
      // Return phone number without spaces
      if (!phoneNumber) return '';
      return phoneNumber.replace(/\s/g, '');
    }),
    getPhoneValues: jest.fn((phoneNumber: string) => {
      // Mock parsing phone numbers
      if (!phoneNumber) return null;

      // Extract country code and phone number
      const normalized = phoneNumber.startsWith('+')
        ? phoneNumber
        : `+${phoneNumber}`;

      // Argentina (+54)
      if (normalized.startsWith('+54')) {
        const phone = normalized.replace('+54', '');
        return {
          phone,
          country: {
            name: 'Argentina',
            dialCode: '+54',
            code: 'AR',
            flag: '🇦🇷',
          },
        };
      }

      // USA (+1)
      if (normalized.startsWith('+1')) {
        const phone = normalized.replace('+1', '');
        return {
          phone,
          country: {
            name: 'United States',
            dialCode: '+1',
            code: 'US',
            flag: '🇺🇸',
          },
        };
      }

      return null;
    }),
    getPhoneNumberMaxLength: jest.fn((countryCode?: string) => {
      if (countryCode === 'AR') return 10;
      if (countryCode === 'US') return 12;
      return 15;
    }),
  };
});

describe('InputPhone utils', () => {
  describe('getDialCode', () => {
    it('should return dial code for Argentina', () => {
      const result = getDialCode('AR');
      expect(result).toBe('+54');
    });

    it('should return dial code for United States', () => {
      const result = getDialCode('US');
      expect(result).toBe('+1');
    });

    it('should return empty string for invalid country code', () => {
      const result = getDialCode('XX' as 'AR');
      expect(result).toBe('');
    });
  });

  describe('getCountryInitialValue', () => {
    it('should return default country when value is empty', () => {
      const result = getCountryInitialValue();
      expect(result).toBeTruthy();
      expect(result.code).toBe('AR');
    });

    it('should extract country from value with dial code', () => {
      const result = getCountryInitialValue('+54111234567');
      expect(result).toBeTruthy();
      expect(result.code).toBe('AR');
      expect(result.dialCode).toBe('+54');
    });

    it('should extract country from value without leading plus', () => {
      const result = getCountryInitialValue('54111234567');
      expect(result).toBeTruthy();
      expect(result.code).toBe('AR');
    });

    it('should return default country when value cannot be parsed', () => {
      const result = getCountryInitialValue('invalid');
      expect(result).toBeTruthy();
      expect(result.code).toBe('AR');
    });
  });

  describe('getPhoneInitialValue', () => {
    it('should return empty string when value is empty', () => {
      const result = getPhoneInitialValue();
      expect(result).toBe('');
    });

    it('should extract phone number from value with dial code', () => {
      const result = getPhoneInitialValue('+54111234567');
      expect(result).toBe('111234567');
    });

    it('should extract phone number from value without leading plus', () => {
      const result = getPhoneInitialValue('54111234567');
      expect(result).toBe('111234567');
    });

    it('should return empty string when value cannot be parsed', () => {
      const result = getPhoneInitialValue('invalid');
      expect(result).toBe('');
    });
  });

  describe('getPhoneMaxLength', () => {
    it('should return max length for Argentina', () => {
      const result = getPhoneMaxLength('AR', '111234567');
      expect(result).toBe(10);
    });

    it('should return max length for United States', () => {
      const result = getPhoneMaxLength('US', '1234567890');
      expect(result).toBe(12);
    });

    it('should return default max length when country code is invalid', () => {
      const result = getPhoneMaxLength('XX' as 'AR', '123');
      expect(result).toBe(15);
    });

    it('should handle errors gracefully', () => {
      const result = getPhoneMaxLength('AR', '');
      expect(result).toBe(10);
    });
  });
});
