import { META } from './metadata';
/**
 * Represents a parsed phone number.
 */
export class PhoneNumber {
  /**
   * Creates a new PhoneNumber instance.
   *
   * @param {string} countryCallingCode - The country calling code for the phone number.
   * @param {string} nationalNumber - The national portion of the phone number.
   * @param {VegaCountryCode} country - The optional country code associated with the number.
   */
  constructor(countryCallingCode, nationalNumber, country) {
    this.countryCallingCode = countryCallingCode;
    this.nationalNumber = nationalNumber;
    this.country = country;
  }
  /**
   * Returns true if the national number matches the country's validation
   * pattern and its length is among the expected possible lengths.
   *
   * @returns {boolean} Whether the phone number is valid.
   */
  isValid() {
    if (!this.country)
      return false;
    const entry = META.countries[this.country];
    if (!entry)
      return false;
    const validationPattern = entry[2];
    const possibleLengths = entry[3];
    if (!possibleLengths.includes(this.nationalNumber.length)) {
      return false;
    }
    const regex = new RegExp('^(?:' + validationPattern + ')$');
    return regex.test(this.nationalNumber);
  }
}
