/**
 * Extracts the country code from an international phone number and returns the separated components.
 *
 * This function:
 * 1. Cleans the phone number by removing non-numeric characters except '+'
 * 2. Uses a comprehensive regex to match international country codes
 * 3. Returns the country code and the remaining national number
 *
 * @param phoneNumber - The international phone number to parse (e.g., "+1 (123) 456-7890")
 * @returns A tuple containing [countryCode, nationalNumber]
 *          If no country code is detected, returns ['', originalPhoneNumber]
 *
 * @example
 * // Returns: ['+1', '1234567890']
 * getCountryCode('+1 (123) 456-7890');
 *
 * @example
 * // Returns: ['+44', '7912345678']
 * getCountryCode('+44 7911 123456');
 *
 * @example
 * // Returns: ['', '1234567890'] - No valid country code found
 * getCountryCode('1234567890');
 */
export const getCountryCode = (phoneNumber: string): [string, string] => {
  // Remove all characters except digits and '+' sign
  // This normalizes the number by stripping spaces, parentheses, dashes, etc.
  const optimizedNumber: string = phoneNumber.replace(/[^0-9+]/g, '');

  /**
   * Comprehensive regex to match international country codes
   * Pattern breakdown:
   * \+                - Must start with plus sign
   * (                 - Start capture group for country code
   *   9[976]\d       | - Country codes starting with 99, 97, 96 (e.g., +998 Uzbekistan)
   *   8[987530]\d    | - Country codes starting with 89, 88, 87, 85, 83, 80
   *   6[987]\d       | - Country codes starting with 69, 68, 67
   *   5[90]\d        | - Country codes starting with 59, 50
   *   42\d           | - Country codes starting with 42
   *   3[875]\d       | - Country codes starting with 38, 37, 35
   *   2[98654321]\d  | - Country codes starting with 29, 28, 27, 26, 25, 24, 23, 22, 21
   *   9[8543210]     | - Single digit country codes: 99, 98, 95, 94, 93, 92, 91, 90
   *   8[6421]        | - Single digit country codes: 86, 84, 82, 81
   *   6[6543210]     | - Single digit country codes: 66, 65, 64, 63, 62, 61, 60
   *   5[87654321]    | - Single digit country codes: 58, 57, 56, 55, 54, 53, 52, 51
   *   4[987654310]   | - Single digit country codes: 49, 48, 47, 46, 45, 44, 43, 42, 41, 40
   *   3[9643210]     | - Single digit country codes: 39, 36, 34, 33, 32, 31, 30
   *   2[70]          | - Single digit country codes: 27, 20
   *   7              | - Country code: +7 (Russia, Kazakhstan)
   *   1              | - Country code: +1 (North America, Caribbean)
   * )                 - End capture group
   * \d{1,14}         - 1 to 14 additional digits for the national number
   * $                 - End of string
   */
  const countryCodeRegex =
    /\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$/;

  // Attempt to match the regex pattern against the optimized number
  const match = optimizedNumber.match(countryCodeRegex);

  // If a match is found and contains the country code capture group
  if (match && match.length > 1) {
    // Extract the country code and add the '+' prefix back
    const countryCode = `+${match[1]}`;

    // Remove the country code from the original phone number and clean up spaces
    const nationalNumber = phoneNumber
      .replace(countryCode, '')
      .replace(' ', '');

    return [countryCode, nationalNumber];
  }

  // Return empty country code and original number if no valid country code found
  return ['', phoneNumber];
};
