{"version":3,"sources":["../source/phoneInputHelpers.js"],"names":["getPreSelectedCountry","phoneNumber","country","countries","includeInternationalOption","metadata","indexOf","undefined","length","getCountrySelectOptions","country_names","country_select_options","map","value","label","sort","a","b","compare_strings","unshift","ZZ","parsePhoneNumber","generateNationalNumberDigits","formatNational","replace","migrateParsedInputForNewCountry","previous_country","new_country","preferNationalFormat","strip_country_calling_code","defaultValue","e164","number","partial_national_significant_number","getNationalSignificantNumberDigits","trimNumber","nationalSignificantNumberPart","overflowDigitsCount","getMaxNumberLength","slice","Metadata","possibleLengths","getCountryForPartialE164Number","partialE164Number","derived_country","get_country_from_possibly_incomplete_international_phone_number","could_number_belong_to_country","parseInput","input","prevInput","defaultCountry","international","limitMaxLength","formatter","AsYouType","String","prototype","localeCompare","country_calling_prefix","Object","keys","country_calling_codes","country_calling_code","getNumber","nationalNumber","i","getInitialParsedInput"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;AAiBO,SAASA,qBAAT,CAA+BC,WAA/B,EAA4CC,OAA5C,EAAqDC,SAArD,EAAgEC,0BAAhE,EAA4FC,QAA5F,EACP;AACC;AACA;AACA,MAAIJ,WAAW,IAAIA,WAAW,CAACC,OAA/B,EACA;AACC;AACAA,IAAAA,OAAO,GAAGD,WAAW,CAACC,OAAtB;AACA,GAPF,CASC;;;AACA,MAAIC,SAAS,IAAIA,SAAS,CAACG,OAAV,CAAkBJ,OAAlB,IAA6B,CAA9C,EACA;AACCA,IAAAA,OAAO,GAAGK,SAAV;AACA,GAbF,CAeC;AACA;AACA;AACA;;;AACA,MAAI,CAACL,OAAD,IAAY,CAACE,0BAAb,IAA2CD,SAA3C,IAAwDA,SAAS,CAACK,MAAV,GAAmB,CAA/E,EACA;AACCN,IAAAA,OAAO,GAAGC,SAAS,CAAC,CAAD,CAAnB;AACA;;AAED,SAAOD,OAAP;AACA;AAED;;;;;;;;;AAOO,SAASO,uBAAT,CAAiCN,SAAjC,EAA4CO,aAA5C,EAA2DN,0BAA3D,EACP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phonenumber-js/core'\r\n\r\n/**\r\n * Decides which country should be pre-selected\r\n * when the phone number input component is first mounted.\r\n * @param {object?} phoneNumber - An instance of `PhoneNumber` class.\r\n * @param {string?} country - Pre-defined country (two-letter code).\r\n * @param {string[]?} countries - A list of countries available.\r\n * @param {boolean} includeInternationalOption - Whether \"International\" country option is available.\r\n * @param {object} metadata - `libphonenumber-js` metadata\r\n * @return {string?}\r\n */\r\nexport function getPreSelectedCountry(phoneNumber, country, countries, includeInternationalOption, metadata)\r\n{\r\n\t// If can get country from E.164 phone number\r\n\t// then it overrides the `country` passed (or not passed).\r\n\tif (phoneNumber && phoneNumber.country)\r\n\t{\r\n\t\t// `country` will be left `undefined` in case of non-detection.\r\n\t\tcountry = phoneNumber.country\r\n\t}\r\n\r\n\t// Only pre-select a country if it's in the available `countries` list.\r\n\tif (countries && countries.indexOf(country) < 0)\r\n\t{\r\n\t\tcountry = undefined\r\n\t}\r\n\r\n\t// If there will be no \"International\" option\r\n\t// then some `country` must be selected.\r\n\t// It will still be the wrong country though.\r\n\t// But still country `` options.\r\n * @param {string[]} countries - A list of two-letter (\"ISO 3166-1 alpha-2\") country codes.\r\n * @param {object} labels - Custom country labels. E.g. `{ RU: 'Россия', US: 'США', ... }`.\r\n * @param {boolean} includeInternationalOption - Whether should include \"International\" option at the top of the list.\r\n * @return {object[]} A list of objects having shape `{ value : string, label : string }`.\r\n */\r\nexport function getCountrySelectOptions(countries, country_names, includeInternationalOption)\r\n{\r\n\t// Generates a `` `value` for the newly selected `country`.\r\n * @param {string?} value - The `value` parsed from phone number `` (it's the `parsed_input` state property, not the `value` property).\r\n * @param {string?} previousCountry - Previously selected country.\r\n * @param {string?} newCountry - Newly selected country. Can't be same as previously selected country.\r\n * @param {object} metadata - `libphonenumber-js` metadata.\r\n * @param {boolean} preferNationalFormat - whether should attempt to convert from international to national number for the new country.\r\n * @return {string?}\r\n */\r\nexport function migrateParsedInputForNewCountry\r\n(\r\n\tvalue,\r\n\tprevious_country,\r\n\tnew_country,\r\n\tmetadata,\r\n\tpreferNationalFormat\r\n)\r\n{\r\n\t// If `parsed_input` is empty\r\n\t// then no need to migrate anything.\r\n\tif (!value) {\r\n\t\tif (preferNationalFormat) {\r\n\t\t\treturn ''\r\n\t\t} else {\r\n\t\t\t// If `parsedInput` is empty then set `parsedInput` to\r\n\t\t\t// `+{getCountryCallingCode(newCountry)}`.\r\n\t\t\treturn '+' + getCountryCallingCode(new_country, metadata)\r\n\t\t}\r\n\t}\r\n\r\n\t// If switching to some country.\r\n\t// (from \"International\" or another country)\r\n\t// If switching from \"International\" then `value` starts with a `+`.\r\n\t// Otherwise it may or may not start with a `+`.\r\n\tif (new_country)\r\n\t{\r\n\t\t// If the phone number was entered in international format\r\n\t\t// then migrate it to the newly selected country.\r\n\t\t// The phone number may be incomplete.\r\n\t\t// The phone number entered not necessarily starts with\r\n\t\t// the previously selected country phone prefix.\r\n\t\tif (value[0] === '+')\r\n\t\t{\r\n\t\t\t// If the international phone number is for the new country\r\n\t\t\t// then convert it to local if required.\r\n\t\t\tif (preferNationalFormat)\r\n\t\t\t{\r\n\t\t\t\t// // If a phone number is being input in international form\r\n\t\t\t\t// // and the country can already be derived from it,\r\n\t\t\t\t// // and if it is the new country, then format as a national number.\r\n\t\t\t\t// const derived_country = get_country_from_possibly_incomplete_international_phone_number(value, metadata)\r\n\t\t\t\t// if (derived_country === new_country)\r\n\t\t\t\t// {\r\n\t\t\t\t// \treturn strip_country_calling_code(value, derived_country, metadata)\r\n\t\t\t\t// }\r\n\r\n\t\t\t\t// Actually, the two countries don't necessarily need to match:\r\n\t\t\t\t// the condition could be looser here, because several countries\r\n\t\t\t\t// might share the same international phone number format\r\n\t\t\t\t// (for example, \"NANPA\" countries like US, Canada, etc).\r\n\t\t\t\t// The looser condition would be just \"same nternational phone number format\"\r\n\t\t\t\t// which would mean \"same country calling code\" in the context of `libphonenumber-js`.\r\n\t\t\t\tif (value.indexOf('+' + getCountryCallingCode(new_country, metadata)) === 0)\r\n\t\t\t\t{\r\n\t\t\t\t\treturn strip_country_calling_code(value, new_country, metadata)\r\n\t\t\t\t}\r\n\r\n\t\t\t\t// Simply discard the previously entered international phone number,\r\n\t\t\t\t// because otherwise any \"smart\" transformation like getting the\r\n\t\t\t\t// \"national (significant) number\" part and then prepending the\r\n\t\t\t\t// newly selected country's \"country calling code\" to it\r\n\t\t\t\t// would just be confusing for a user without being actually useful.\r\n\t\t\t\treturn ''\r\n\r\n\t\t\t\t// // Simply strip the leading `+` character\r\n\t\t\t\t// // therefore simply converting all digits into a \"local\" phone number.\r\n\t\t\t\t// // https://github.com/catamphetamine/react-phone-number-input/issues/287\r\n\t\t\t\t// return value.slice(1)\r\n\t\t\t}\r\n\r\n\t\t\tif (previous_country) {\r\n\t\t\t\tif (getCountryCallingCode(new_country, metadata) === getCountryCallingCode(previous_country, metadata)) {\r\n\t\t\t\t\treturn value\r\n\t\t\t\t} else {\r\n\t\t\t\t\treturn `+${getCountryCallingCode(new_country, metadata)}`\r\n\t\t\t\t}\r\n\t\t\t} else {\r\n\t\t\t\tconst defaultValue = `+${getCountryCallingCode(new_country, metadata)}`\r\n\t\t\t\t// If `parsedInput`'s country calling code part is the same\r\n\t\t\t\t// as for the new `country`, then leave `parsedInput` as is.\r\n\t\t\t\tif (value.indexOf(defaultValue) === 0) {\r\n\t\t\t\t\treturn value\r\n\t\t\t\t}\r\n\t\t\t\t// If `parsedInput`'s country calling code part is not the same\r\n\t\t\t\t// as for the new `country`, then set `parsedInput` to\r\n\t\t\t\t// `+{getCountryCallingCode(newCountry)}`.\r\n\t\t\t\treturn defaultValue\r\n\t\t\t}\r\n\r\n\t\t\t// // If the international phone number already contains\r\n\t\t\t// // any country calling code then trim the country calling code part.\r\n\t\t\t// // (that could also be the newly selected country phone code prefix as well)\r\n\t\t\t// // `value` doesn't neccessarily belong to `previous_country`.\r\n\t\t\t// // (e.g. if a user enters an international number\r\n\t\t\t// // not belonging to any of the reduced `countries` list).\r\n\t\t\t// value = strip_country_calling_code(value, previous_country, metadata)\r\n\r\n\t\t\t// // Prepend country calling code prefix\r\n\t\t\t// // for the newly selected country.\r\n\t\t\t// return e164(value, new_country, metadata) || `+${getCountryCallingCode(new_country, metadata)}`\r\n\t\t}\r\n\t}\r\n\t// If switching to \"International\" from a country.\r\n\telse\r\n\t{\r\n\t\t// If the phone number was entered in national format.\r\n\t\tif (value[0] !== '+')\r\n\t\t{\r\n\t\t\t// Format the national phone number as an international one.\r\n\t\t\t// The phone number entered not necessarily even starts with\r\n\t\t\t// the previously selected country phone prefix.\r\n\t\t\t// Even if the phone number belongs to whole another country\r\n\t\t\t// it will still be parsed into some national phone number.\r\n\t\t\treturn e164(value, previous_country, metadata) || ''\r\n\t\t}\r\n\t}\r\n\r\n\treturn value\r\n}\r\n\r\n/**\r\n * Converts phone number digits to a (possibly incomplete) E.164 phone number.\r\n * @param {string?} number - A possibly incomplete phone number digits string. Can be a possibly incomplete E.164 phone number.\r\n * @param {string?} country\r\n * @param {[object} metadata - `libphonenumber-js` metadata.\r\n * @return {string?}\r\n */\r\nexport function e164(number, country, metadata) {\r\n\tif (!number) {\r\n\t\treturn\r\n\t}\r\n\t// If the phone number is being input in international format.\r\n\tif (number[0] === '+') {\r\n\t\t// If it's just the `+` sign then return nothing.\r\n\t\tif (number === '+') {\r\n\t\t\treturn\r\n\t\t}\r\n\t\t// If there are any digits then the `value` is returned as is.\r\n\t\treturn number\r\n\t}\r\n\t// For non-international phone numbers\r\n\t// an accompanying country code is required.\r\n\tif (!country) {\r\n\t\treturn\r\n\t}\r\n\tconst partial_national_significant_number = getNationalSignificantNumberDigits(number, country, metadata)\r\n\tif (partial_national_significant_number) {\r\n\t\treturn `+${getCountryCallingCode(country, metadata)}${partial_national_significant_number}`\r\n\t}\r\n}\r\n\r\n/**\r\n * Trims phone number digits if they exceed the maximum possible length\r\n * for a national (significant) number for the country.\r\n * @param {string} number - A possibly incomplete phone number digits string. Can be a possibly incomplete E.164 phone number.\r\n * @param {string} country\r\n * @param {object} metadata - `libphonenumber-js` metadata.\r\n * @return {string} Can be empty.\r\n */\r\nexport function trimNumber(number, country, metadata)\r\n{\r\n\tconst nationalSignificantNumberPart = getNationalSignificantNumberDigits(number, country, metadata)\r\n\tif (nationalSignificantNumberPart) {\r\n\t\tconst overflowDigitsCount = nationalSignificantNumberPart.length - getMaxNumberLength(country, metadata)\r\n\t\tif (overflowDigitsCount > 0) {\r\n\t\t\treturn number.slice(0, number.length - overflowDigitsCount)\r\n\t\t}\r\n\t}\r\n\treturn number\r\n}\r\n\r\nfunction getMaxNumberLength(country, metadata)\r\n{\r\n\t// Get \"possible lengths\" for a phone number of the country.\r\n\tmetadata = new Metadata(metadata)\r\n\tmetadata.country(country)\r\n\t// Return the last \"possible length\".\r\n\treturn metadata.possibleLengths()[metadata.possibleLengths().length - 1]\r\n}\r\n\r\n// If the phone number being input is an international one\r\n// then tries to derive the country from the phone number.\r\n// (regardless of whether there's any country currently selected)\r\n/**\r\n * @param {string} parsedInput - A possibly incomplete E.164 phone number.\r\n * @param {string?} country - Currently selected country.\r\n * @param {string[]?} countries - A list of available countries. If not passed then \"all countries\" are assumed.\r\n * @param {boolean} includeInternationalOption - Whether \"International\" country option is available.\r\n * @param {object} metadata - `libphonenumber-js` metadata.\r\n * @return {string?}\r\n */\r\nexport function getCountryForPartialE164Number\r\n(\r\n\tpartialE164Number,\r\n\tcountry,\r\n\tcountries,\r\n\tincludeInternationalOption,\r\n\tmetadata\r\n)\r\n{\r\n\tif (partialE164Number === '+')\r\n\t{\r\n\t\t// Don't change the currently selected country yet.\r\n\t\treturn country\r\n\t}\r\n\r\n\tconst derived_country = get_country_from_possibly_incomplete_international_phone_number(partialE164Number, metadata)\r\n\r\n\t// If a phone number is being input in international form\r\n\t// and the country can already be derived from it,\r\n\t// then select that country.\r\n\tif (derived_country && (!countries || (countries.indexOf(derived_country) >= 0)))\r\n\t{\r\n\t\treturn derived_country\r\n\t}\r\n\t// If \"International\" country option has not been disabled\r\n\t// and the international phone number entered doesn't correspond\r\n\t// to the currently selected country then reset the currently selected country.\r\n\telse if (country &&\r\n\t\tincludeInternationalOption &&\r\n\t\t!could_number_belong_to_country(partialE164Number, country, metadata))\r\n\t{\r\n\t\treturn undefined\r\n\t}\r\n\r\n\t// Don't change the currently selected country.\r\n\treturn country\r\n}\r\n\r\n/**\r\n * Parses `` value. Derives `country` from `input`. Derives an E.164 `value`.\r\n * @param {string?} input — Parsed `` value. Examples: `\"\"`, `\"+\"`, `\"+123\"`, `\"123\"`.\r\n * @param {string?} prevInput — Previous parsed `` value. Examples: `\"\"`, `\"+\"`, `\"+123\"`, `\"123\"`.\r\n * @param {string?} country - Currently selected country.\r\n * @param {string[]?} countries - A list of available countries. If not passed then \"all countries\" are assumed.\r\n * @param {boolean} includeInternationalOption - Whether \"International\" country option is available.\r\n * @param {boolean} international - Set to `true` to force international phone number format (leading `+`).\r\n * @param {boolean} limitMaxLength — Whether to enable limiting phone number max length.\r\n * @param {object} metadata - `libphonenumber-js` metadata.\r\n * @return {object} An object of shape `{ input, country, value }`.\r\n */\r\nexport function parseInput(\r\n\tinput,\r\n\tprevInput,\r\n\tcountry,\r\n\tdefaultCountry,\r\n\tcountries,\r\n\tincludeInternationalOption,\r\n\tinternational,\r\n\tlimitMaxLength,\r\n\tmetadata\r\n) {\r\n\t// Trim the input to not exceed the maximum possible number length.\r\n\tif (input && country && limitMaxLength) {\r\n\t\tinput = trimNumber(input, country, metadata)\r\n\t}\r\n\r\n\t// If this `onChange()` event was triggered\r\n\t// as a result of selecting \"International\" country\r\n\t// then force-prepend a `+` sign if the phone number\r\n\t// `` value isn't in international format.\r\n\t// Also, force-prepend a `+` sign if international\r\n\t// phone number input format is set.\r\n\tif (input && input[0] !== '+' && (!country || international)) {\r\n\t\tinput = '+' + input\r\n\t}\r\n\r\n\t// If the previously entered phone number\r\n\t// has been entered in international format\r\n\t// and the user decides to erase it,\r\n\t// then also reset the `country`\r\n\t// because it was most likely automatically selected\r\n\t// while the user was typing in the phone number\r\n\t// in international format.\r\n\t// This fixes the issue when a user is presented\r\n\t// with a phone number input with no country selected\r\n\t// and then types in their local phone number\r\n\t// then discovers that the input's messed up\r\n\t// (a `+` has been prepended at the start of their input\r\n\t// and a random country has been selected),\r\n\t// decides to undo it all by erasing everything\r\n\t// and then types in their local phone number again\r\n\t// resulting in a seemingly correct phone number\r\n\t// but in reality that phone number has incorrect country.\r\n\t// https://github.com/catamphetamine/react-phone-number-input/issues/273\r\n\tif (!input && prevInput && prevInput[0] === '+') {\r\n\t\tif (international) {\r\n\t\t\tcountry = undefined\r\n\t\t} else {\r\n\t\t\tcountry = defaultCountry\r\n\t\t}\r\n\t}\r\n\t// Also resets such \"randomly\" selected country\r\n\t// as soon as the user erases the number\r\n\t// digit-by-digit up to the leading `+` sign.\r\n\tif (input === '+' && prevInput && prevInput[0] === '+' && prevInput.length > '+'.length) {\r\n\t\tcountry = undefined\r\n\t}\r\n\r\n\t// Generate the new `value` property.\r\n\tlet value\r\n\tif (input) {\r\n\t\tif (input[0] === '+') {\r\n\t\t\tif (input !== '+') {\r\n\t\t\t\tvalue = input\r\n\t\t\t}\r\n\t\t} else {\r\n\t\t\tvalue = e164(input, country, metadata)\r\n\t\t}\r\n\t}\r\n\r\n\t// Derive the country from the phone number.\r\n\t// (regardless of whether there's any country currently selected)\r\n\tif (value) {\r\n\t\tcountry = getCountryForPartialE164Number(\r\n\t\t\tvalue,\r\n\t\t\tcountry,\r\n\t\t\tcountries,\r\n\t\t\tincludeInternationalOption,\r\n\t\t\tmetadata\r\n\t\t)\r\n\t}\r\n\r\n\treturn {\r\n\t\tinput,\r\n\t\tcountry,\r\n\t\tvalue\r\n\t}\r\n}\r\n\r\n/**\r\n * Determines the country for a given (possibly incomplete) E.164 phone number.\r\n * @param {string} number - A possibly incomplete E.164 phone number.\r\n * @param {object} metadata - `libphonenumber-js` metadata.\r\n * @return {string?}\r\n */\r\nexport function get_country_from_possibly_incomplete_international_phone_number(number, metadata)\r\n{\r\n\tconst formatter = new AsYouType(null, metadata)\r\n\tformatter.input(number)\r\n\t// // `001` is a special \"non-geograpical entity\" code\r\n\t// // in Google's `libphonenumber` library.\r\n\t// if (formatter.country === '001') {\r\n\t// \treturn\r\n\t// }\r\n\treturn formatter.country\r\n}\r\n\r\n/**\r\n * Compares two strings.\r\n * A helper for `Array.sort()`.\r\n */\r\nexport function compare_strings(a, b) {\r\n // Use `String.localeCompare` if it's available.\r\n // https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare\r\n // Which means everyone except IE <= 10 and Safari <= 10.\r\n // `localeCompare()` is available in latest Node.js versions.\r\n /* istanbul ignore else */\r\n if (String.prototype.localeCompare) {\r\n return a.localeCompare(b);\r\n }\r\n /* istanbul ignore next */\r\n return a < b ? -1 : (a > b ? 1 : 0);\r\n}\r\n\r\n/**\r\n * Strips `+${countryCallingCode}` prefix from an E.164 phone number.\r\n * @param {string} number - (possibly incomplete) E.164 phone number.\r\n * @param {string?} country - A possible country for this phone number.\r\n * @param {object} metadata - `libphonenumber-js` metadata.\r\n * @return {string}\r\n */\r\nexport function strip_country_calling_code(number, country, metadata)\r\n{\r\n\t// Just an optimization, so that it\r\n\t// doesn't have to iterate through all country calling codes.\r\n\tif (country)\r\n\t{\r\n\t\tconst country_calling_prefix = '+' + getCountryCallingCode(country, metadata)\r\n\r\n\t\t// If `country` fits the actual `number`.\r\n\t\tif (number.length < country_calling_prefix.length)\r\n\t\t{\r\n\t\t\tif (country_calling_prefix.indexOf(number) === 0)\r\n\t\t\t{\r\n\t\t\t\treturn ''\r\n\t\t\t}\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tif (number.indexOf(country_calling_prefix) === 0)\r\n\t\t\t{\r\n\t\t\t\treturn number.slice(country_calling_prefix.length)\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\t// If `country` doesn't fit the actual `number`.\r\n\t// Try all available country calling codes.\r\n\tfor (const country_calling_code of Object.keys(metadata.country_calling_codes))\r\n\t{\r\n\t\tif (number.indexOf(country_calling_code) === '+'.length)\r\n\t\t{\r\n\t\t\treturn number.slice('+'.length + country_calling_code.length)\r\n\t\t}\r\n\t}\r\n\r\n\treturn ''\r\n}\r\n\r\n/**\r\n * Parses a partially entered national phone number digits\r\n * (or a partially entered E.164 international phone number)\r\n * and returns the national significant number part.\r\n * National significant number returned doesn't come with a national prefix.\r\n * @param {string} number - National number digits. Or possibly incomplete E.164 phone number.\r\n * @param {string?} country\r\n * @param {object} metadata - `libphonenumber-js` metadata.\r\n * @return {string} [result]\r\n */\r\nexport function getNationalSignificantNumberDigits(number, country, metadata) {\r\n\t// Create \"as you type\" formatter.\r\n\tconst formatter = new AsYouType(country, metadata)\r\n\t// Input partial national phone number.\r\n\tformatter.input(number)\r\n\t// Return the parsed partial national phone number.\r\n\tconst phoneNumber = formatter.getNumber()\r\n\treturn phoneNumber && phoneNumber.nationalNumber\r\n}\r\n\r\n/**\r\n * Checks if a partially entered E.164 phone number could belong to a country.\r\n * @param {string} number\r\n * @param {string} country\r\n * @return {boolean}\r\n */\r\nexport function could_number_belong_to_country(number, country, metadata)\r\n{\r\n\tconst country_calling_code = getCountryCallingCode(country, metadata)\r\n\r\n\tlet i = 0\r\n\twhile (i + 1 < number.length && i < country_calling_code.length)\r\n\t{\r\n\t\tif (number[i + 1] !== country_calling_code[i])\r\n\t\t{\r\n\t\t\treturn false\r\n\t\t}\r\n\t\ti++\r\n\t}\r\n\r\n\treturn true\r\n}\r\n\r\nexport function getInitialParsedInput(value, country, international, metadata) {\r\n\t// If `international` property is `true`,\r\n\t// then always show country calling code in the input field.\r\n\tif (!value && international && country) {\r\n\t\treturn `+${getCountryCallingCode(country, metadata)}`\r\n\t}\r\n\treturn value\r\n}"],"file":"phoneInputHelpers.js"}