const require_rolldown_runtime = require('../../../../../../_virtual/rolldown_runtime.js');
const require_constants$1 = require('./constants.js');
const require_parseDigits$1 = require('./helpers/parseDigits.js');
const require_stripIddPrefix$1 = require('./helpers/stripIddPrefix.js');
const require_extractNationalNumberFromPossiblyIncompleteNumber$1 = require('./helpers/extractNationalNumberFromPossiblyIncompleteNumber.js');
const require_extractCountryCallingCodeFromInternationalNumberWithoutPlusSign$1 = require('./helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js');
const require_extractCountryCallingCode$1 = require('./helpers/extractCountryCallingCode.js');

//#region ../../node_modules/.pnpm/libphonenumber-js@1.11.3/node_modules/libphonenumber-js/build/AsYouTypeParser.js
var require_AsYouTypeParser = /* @__PURE__ */ require_rolldown_runtime.__commonJS({ "../../node_modules/.pnpm/libphonenumber-js@1.11.3/node_modules/libphonenumber-js/build/AsYouTypeParser.js": ((exports) => {
	Object.defineProperty(exports, "__esModule", { value: true });
	exports.extractFormattedDigitsAndPlus = extractFormattedDigitsAndPlus;
	var _extractCountryCallingCode2 = _interopRequireDefault(require_extractCountryCallingCode$1.require_extractCountryCallingCode());
	var _extractCountryCallingCodeFromInternationalNumberWithoutPlusSign = _interopRequireDefault(require_extractCountryCallingCodeFromInternationalNumberWithoutPlusSign$1.require_extractCountryCallingCodeFromInternationalNumberWithoutPlusSign());
	var _extractNationalNumberFromPossiblyIncompleteNumber = _interopRequireDefault(require_extractNationalNumberFromPossiblyIncompleteNumber$1.require_extractNationalNumberFromPossiblyIncompleteNumber());
	var _stripIddPrefix = _interopRequireDefault(require_stripIddPrefix$1.require_stripIddPrefix());
	var _parseDigits = _interopRequireDefault(require_parseDigits$1.require_parseDigits());
	var _constants = require_constants$1.require_constants();
	function _interopRequireDefault(obj) {
		return obj && obj.__esModule ? obj : { "default": obj };
	}
	function _slicedToArray(arr, i) {
		return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
	}
	function _nonIterableRest() {
		throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
	}
	function _unsupportedIterableToArray(o, minLen) {
		if (!o) return;
		if (typeof o === "string") return _arrayLikeToArray(o, minLen);
		var n = Object.prototype.toString.call(o).slice(8, -1);
		if (n === "Object" && o.constructor) n = o.constructor.name;
		if (n === "Map" || n === "Set") return Array.from(o);
		if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
	}
	function _arrayLikeToArray(arr, len) {
		if (len == null || len > arr.length) len = arr.length;
		for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
		return arr2;
	}
	function _iterableToArrayLimit(arr, i) {
		var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
		if (_i == null) return;
		var _arr = [];
		var _n = true;
		var _d = false;
		var _s, _e;
		try {
			for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
				_arr.push(_s.value);
				if (i && _arr.length === i) break;
			}
		} catch (err) {
			_d = true;
			_e = err;
		} finally {
			try {
				if (!_n && _i["return"] != null) _i["return"]();
			} finally {
				if (_d) throw _e;
			}
		}
		return _arr;
	}
	function _arrayWithHoles(arr) {
		if (Array.isArray(arr)) return arr;
	}
	function _classCallCheck(instance, Constructor) {
		if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function");
	}
	function _defineProperties(target, props) {
		for (var i = 0; i < props.length; i++) {
			var descriptor = props[i];
			descriptor.enumerable = descriptor.enumerable || false;
			descriptor.configurable = true;
			if ("value" in descriptor) descriptor.writable = true;
			Object.defineProperty(target, descriptor.key, descriptor);
		}
	}
	function _createClass(Constructor, protoProps, staticProps) {
		if (protoProps) _defineProperties(Constructor.prototype, protoProps);
		if (staticProps) _defineProperties(Constructor, staticProps);
		Object.defineProperty(Constructor, "prototype", { writable: false });
		return Constructor;
	}
	var VALID_FORMATTED_PHONE_NUMBER_DIGITS_PART = "[" + _constants.VALID_PUNCTUATION + _constants.VALID_DIGITS + "]+";
	var VALID_FORMATTED_PHONE_NUMBER_DIGITS_PART_PATTERN = new RegExp("^" + VALID_FORMATTED_PHONE_NUMBER_DIGITS_PART + "$", "i");
	var VALID_FORMATTED_PHONE_NUMBER_PART = "(?:[" + _constants.PLUS_CHARS + "][" + _constants.VALID_PUNCTUATION + _constants.VALID_DIGITS + "]*|[" + _constants.VALID_PUNCTUATION + _constants.VALID_DIGITS + "]+)";
	var AFTER_PHONE_NUMBER_DIGITS_END_PATTERN = /* @__PURE__ */ new RegExp("[^" + _constants.VALID_PUNCTUATION + _constants.VALID_DIGITS + "]+.*$");
	var COMPLEX_NATIONAL_PREFIX = /[^\d\[\]]/;
	var AsYouTypeParser = /* @__PURE__ */ function() {
		function AsYouTypeParser$1(_ref) {
			var defaultCountry = _ref.defaultCountry, defaultCallingCode = _ref.defaultCallingCode, metadata = _ref.metadata, onNationalSignificantNumberChange = _ref.onNationalSignificantNumberChange;
			_classCallCheck(this, AsYouTypeParser$1);
			this.defaultCountry = defaultCountry;
			this.defaultCallingCode = defaultCallingCode;
			this.metadata = metadata;
			this.onNationalSignificantNumberChange = onNationalSignificantNumberChange;
		}
		_createClass(AsYouTypeParser$1, [
			{
				key: "input",
				value: function input(text, state) {
					var _extractFormattedDigi2 = _slicedToArray(extractFormattedDigitsAndPlus(text), 2), formattedDigits = _extractFormattedDigi2[0], hasPlus = _extractFormattedDigi2[1];
					var digits = (0, _parseDigits["default"])(formattedDigits);
					var justLeadingPlus;
					if (hasPlus) {
						if (!state.digits) {
							state.startInternationalNumber();
							if (!digits) justLeadingPlus = true;
						}
					}
					if (digits) this.inputDigits(digits, state);
					return {
						digits,
						justLeadingPlus
					};
				}
			},
			{
				key: "inputDigits",
				value: function inputDigits(nextDigits, state) {
					var digits = state.digits;
					var hasReceivedThreeLeadingDigits = digits.length < 3 && digits.length + nextDigits.length >= 3;
					state.appendDigits(nextDigits);
					if (hasReceivedThreeLeadingDigits) this.extractIddPrefix(state);
					if (this.isWaitingForCountryCallingCode(state)) {
						if (!this.extractCountryCallingCode(state)) return;
					} else state.appendNationalSignificantNumberDigits(nextDigits);
					if (!state.international) {
						if (!this.hasExtractedNationalSignificantNumber) this.extractNationalSignificantNumber(state.getNationalDigits(), function(stateUpdate) {
							return state.update(stateUpdate);
						});
					}
				}
			},
			{
				key: "isWaitingForCountryCallingCode",
				value: function isWaitingForCountryCallingCode(_ref2) {
					var international = _ref2.international, callingCode = _ref2.callingCode;
					return international && !callingCode;
				}
			},
			{
				key: "extractCountryCallingCode",
				value: function extractCountryCallingCode(state) {
					var _extractCountryCallin = (0, _extractCountryCallingCode2["default"])("+" + state.getDigitsWithoutInternationalPrefix(), this.defaultCountry, this.defaultCallingCode, this.metadata.metadata), countryCallingCode = _extractCountryCallin.countryCallingCode, number = _extractCountryCallin.number;
					if (countryCallingCode) {
						state.setCallingCode(countryCallingCode);
						state.update({ nationalSignificantNumber: number });
						return true;
					}
				}
			},
			{
				key: "reset",
				value: function reset(numberingPlan) {
					if (numberingPlan) {
						this.hasSelectedNumberingPlan = true;
						var nationalPrefixForParsing = numberingPlan._nationalPrefixForParsing();
						this.couldPossiblyExtractAnotherNationalSignificantNumber = nationalPrefixForParsing && COMPLEX_NATIONAL_PREFIX.test(nationalPrefixForParsing);
					} else {
						this.hasSelectedNumberingPlan = void 0;
						this.couldPossiblyExtractAnotherNationalSignificantNumber = void 0;
					}
				}
			},
			{
				key: "extractNationalSignificantNumber",
				value: function extractNationalSignificantNumber(nationalDigits, setState) {
					if (!this.hasSelectedNumberingPlan) return;
					var _extractNationalNumbe = (0, _extractNationalNumberFromPossiblyIncompleteNumber["default"])(nationalDigits, this.metadata), nationalPrefix = _extractNationalNumbe.nationalPrefix, nationalNumber = _extractNationalNumbe.nationalNumber, carrierCode = _extractNationalNumbe.carrierCode;
					if (nationalNumber === nationalDigits) return;
					this.onExtractedNationalNumber(nationalPrefix, carrierCode, nationalNumber, nationalDigits, setState);
					return true;
				}
			},
			{
				key: "extractAnotherNationalSignificantNumber",
				value: function extractAnotherNationalSignificantNumber(nationalDigits, prevNationalSignificantNumber, setState) {
					if (!this.hasExtractedNationalSignificantNumber) return this.extractNationalSignificantNumber(nationalDigits, setState);
					if (!this.couldPossiblyExtractAnotherNationalSignificantNumber) return;
					var _extractNationalNumbe2 = (0, _extractNationalNumberFromPossiblyIncompleteNumber["default"])(nationalDigits, this.metadata), nationalPrefix = _extractNationalNumbe2.nationalPrefix, nationalNumber = _extractNationalNumbe2.nationalNumber, carrierCode = _extractNationalNumbe2.carrierCode;
					/* istanbul ignore if */
					if (nationalNumber === prevNationalSignificantNumber) return;
					this.onExtractedNationalNumber(nationalPrefix, carrierCode, nationalNumber, nationalDigits, setState);
					return true;
				}
			},
			{
				key: "onExtractedNationalNumber",
				value: function onExtractedNationalNumber(nationalPrefix, carrierCode, nationalSignificantNumber, nationalDigits, setState) {
					var complexPrefixBeforeNationalSignificantNumber;
					var nationalSignificantNumberMatchesInput;
					var nationalSignificantNumberIndex = nationalDigits.lastIndexOf(nationalSignificantNumber);
					if (nationalSignificantNumberIndex >= 0 && nationalSignificantNumberIndex === nationalDigits.length - nationalSignificantNumber.length) {
						nationalSignificantNumberMatchesInput = true;
						var prefixBeforeNationalNumber = nationalDigits.slice(0, nationalSignificantNumberIndex);
						if (prefixBeforeNationalNumber !== nationalPrefix) complexPrefixBeforeNationalSignificantNumber = prefixBeforeNationalNumber;
					}
					setState({
						nationalPrefix,
						carrierCode,
						nationalSignificantNumber,
						nationalSignificantNumberMatchesInput,
						complexPrefixBeforeNationalSignificantNumber
					});
					this.hasExtractedNationalSignificantNumber = true;
					this.onNationalSignificantNumberChange();
				}
			},
			{
				key: "reExtractNationalSignificantNumber",
				value: function reExtractNationalSignificantNumber(state) {
					if (this.extractAnotherNationalSignificantNumber(state.getNationalDigits(), state.nationalSignificantNumber, function(stateUpdate) {
						return state.update(stateUpdate);
					})) return true;
					if (this.extractIddPrefix(state)) {
						this.extractCallingCodeAndNationalSignificantNumber(state);
						return true;
					}
					if (this.fixMissingPlus(state)) {
						this.extractCallingCodeAndNationalSignificantNumber(state);
						return true;
					}
				}
			},
			{
				key: "extractIddPrefix",
				value: function extractIddPrefix(state) {
					var international = state.international, IDDPrefix = state.IDDPrefix, digits = state.digits;
					state.nationalSignificantNumber;
					if (international || IDDPrefix) return;
					var numberWithoutIDD = (0, _stripIddPrefix["default"])(digits, this.defaultCountry, this.defaultCallingCode, this.metadata.metadata);
					if (numberWithoutIDD !== void 0 && numberWithoutIDD !== digits) {
						state.update({ IDDPrefix: digits.slice(0, digits.length - numberWithoutIDD.length) });
						this.startInternationalNumber(state, {
							country: void 0,
							callingCode: void 0
						});
						return true;
					}
				}
			},
			{
				key: "fixMissingPlus",
				value: function fixMissingPlus(state) {
					if (!state.international) {
						var _extractCountryCallin2 = (0, _extractCountryCallingCodeFromInternationalNumberWithoutPlusSign["default"])(state.digits, this.defaultCountry, this.defaultCallingCode, this.metadata.metadata), newCallingCode = _extractCountryCallin2.countryCallingCode;
						_extractCountryCallin2.number;
						if (newCallingCode) {
							state.update({ missingPlus: true });
							this.startInternationalNumber(state, {
								country: state.country,
								callingCode: newCallingCode
							});
							return true;
						}
					}
				}
			},
			{
				key: "startInternationalNumber",
				value: function startInternationalNumber(state, _ref3) {
					var country = _ref3.country, callingCode = _ref3.callingCode;
					state.startInternationalNumber(country, callingCode);
					if (state.nationalSignificantNumber) {
						state.resetNationalSignificantNumber();
						this.onNationalSignificantNumberChange();
						this.hasExtractedNationalSignificantNumber = void 0;
					}
				}
			},
			{
				key: "extractCallingCodeAndNationalSignificantNumber",
				value: function extractCallingCodeAndNationalSignificantNumber(state) {
					if (this.extractCountryCallingCode(state)) this.extractNationalSignificantNumber(state.getNationalDigits(), function(stateUpdate) {
						return state.update(stateUpdate);
					});
				}
			}
		]);
		return AsYouTypeParser$1;
	}();
	/**
	* Extracts formatted phone number from text (if there's any).
	* @param  {string} text
	* @return {string} [formattedPhoneNumber]
	*/
	exports["default"] = AsYouTypeParser;
	function extractFormattedPhoneNumber(text) {
		var startsAt = text.search(VALID_FORMATTED_PHONE_NUMBER_PART);
		if (startsAt < 0) return;
		text = text.slice(startsAt);
		var hasPlus;
		if (text[0] === "+") {
			hasPlus = true;
			text = text.slice(1);
		}
		text = text.replace(AFTER_PHONE_NUMBER_DIGITS_END_PATTERN, "");
		if (hasPlus) text = "+" + text;
		return text;
	}
	/**
	* Extracts formatted phone number digits (and a `+`) from text (if there're any).
	* @param  {string} text
	* @return {any[]}
	*/
	function _extractFormattedDigitsAndPlus(text) {
		var extractedNumber = extractFormattedPhoneNumber(text) || "";
		if (extractedNumber[0] === "+") return [extractedNumber.slice(1), true];
		return [extractedNumber];
	}
	/**
	* Extracts formatted phone number digits (and a `+`) from text (if there're any).
	* @param  {string} text
	* @return {any[]}
	*/
	function extractFormattedDigitsAndPlus(text) {
		var _extractFormattedDigi4 = _slicedToArray(_extractFormattedDigitsAndPlus(text), 2), formattedDigits = _extractFormattedDigi4[0], hasPlus = _extractFormattedDigi4[1];
		if (!VALID_FORMATTED_PHONE_NUMBER_DIGITS_PART_PATTERN.test(formattedDigits)) formattedDigits = "";
		return [formattedDigits, hasPlus];
	}
}) });

//#endregion
Object.defineProperty(exports, 'default', {
  enumerable: true,
  get: function () {
    return require_AsYouTypeParser();
  }
});