const require_rolldown_runtime = require('../../../../../../_virtual/rolldown_runtime.js');
const require_constants$1 = require('./constants.js');
const require_ParseError$1 = require('./ParseError.js');
const require_metadata$1 = require('./metadata.js');
const require_isViablePhoneNumber$1 = require('./helpers/isViablePhoneNumber.js');
const require_extractExtension$1 = require('./helpers/extension/extractExtension.js');
const require_parseIncompletePhoneNumber$1 = require('./parseIncompletePhoneNumber.js');
const require_getCountryCallingCode$1 = require('./getCountryCallingCode.js');
const require_isPossible$1 = require('./isPossible.js');
const require_matchesEntirely$1 = require('./helpers/matchesEntirely.js');
const require_PhoneNumber$1 = require('./PhoneNumber.js');
const require_stripIddPrefix$1 = require('./helpers/stripIddPrefix.js');
const require_extractNationalNumber$1 = require('./helpers/extractNationalNumber.js');
const require_extractCountryCallingCode$1 = require('./helpers/extractCountryCallingCode.js');
const require_getCountryByCallingCode$1 = require('./helpers/getCountryByCallingCode.js');
const require_extractFormattedPhoneNumberFromPossibleRfc3966NumberUri$1 = require('./helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js');

//#region ../../node_modules/.pnpm/libphonenumber-js@1.11.3/node_modules/libphonenumber-js/build/parse.js
var require_parse = /* @__PURE__ */ require_rolldown_runtime.__commonJS({ "../../node_modules/.pnpm/libphonenumber-js@1.11.3/node_modules/libphonenumber-js/build/parse.js": ((exports) => {
	function _typeof(obj) {
		"@babel/helpers - typeof";
		return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(obj$1) {
			return typeof obj$1;
		} : function(obj$1) {
			return obj$1 && "function" == typeof Symbol && obj$1.constructor === Symbol && obj$1 !== Symbol.prototype ? "symbol" : typeof obj$1;
		}, _typeof(obj);
	}
	Object.defineProperty(exports, "__esModule", { value: true });
	exports["default"] = parse;
	var _constants = require_constants$1.require_constants();
	var _ParseError = _interopRequireDefault(require_ParseError$1.require_ParseError());
	var _metadata = _interopRequireDefault(require_metadata$1.require_metadata());
	var _isViablePhoneNumber = _interopRequireWildcard(require_isViablePhoneNumber$1.require_isViablePhoneNumber());
	var _extractExtension = _interopRequireDefault(require_extractExtension$1.require_extractExtension());
	var _parseIncompletePhoneNumber = _interopRequireDefault(require_parseIncompletePhoneNumber$1.require_parseIncompletePhoneNumber());
	var _getCountryCallingCode = _interopRequireDefault(require_getCountryCallingCode$1.require_getCountryCallingCode());
	var _isPossible = require_isPossible$1.require_isPossible();
	var _PhoneNumber = _interopRequireDefault(require_PhoneNumber$1.require_PhoneNumber());
	var _matchesEntirely = _interopRequireDefault(require_matchesEntirely$1.require_matchesEntirely());
	var _extractCountryCallingCode = _interopRequireDefault(require_extractCountryCallingCode$1.require_extractCountryCallingCode());
	var _extractNationalNumber = _interopRequireDefault(require_extractNationalNumber$1.require_extractNationalNumber());
	_interopRequireDefault(require_stripIddPrefix$1.require_stripIddPrefix());
	var _getCountryByCallingCode = _interopRequireDefault(require_getCountryByCallingCode$1.require_getCountryByCallingCode());
	var _extractFormattedPhoneNumberFromPossibleRfc3966NumberUri = _interopRequireDefault(require_extractFormattedPhoneNumberFromPossibleRfc3966NumberUri$1.require_extractFormattedPhoneNumberFromPossibleRfc3966NumberUri());
	function _getRequireWildcardCache(nodeInterop) {
		if (typeof WeakMap !== "function") return null;
		var cacheBabelInterop = /* @__PURE__ */ new WeakMap();
		var cacheNodeInterop = /* @__PURE__ */ new WeakMap();
		return (_getRequireWildcardCache = function _getRequireWildcardCache$1(nodeInterop$1) {
			return nodeInterop$1 ? cacheNodeInterop : cacheBabelInterop;
		})(nodeInterop);
	}
	function _interopRequireWildcard(obj, nodeInterop) {
		if (!nodeInterop && obj && obj.__esModule) return obj;
		if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") return { "default": obj };
		var cache = _getRequireWildcardCache(nodeInterop);
		if (cache && cache.has(obj)) return cache.get(obj);
		var newObj = {};
		var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
		for (var key in obj) if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
			var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
			if (desc && (desc.get || desc.set)) Object.defineProperty(newObj, key, desc);
			else newObj[key] = obj[key];
		}
		newObj["default"] = obj;
		if (cache) cache.set(obj, newObj);
		return newObj;
	}
	function _interopRequireDefault(obj) {
		return obj && obj.__esModule ? obj : { "default": obj };
	}
	var MAX_INPUT_STRING_LENGTH = 250;
	var PHONE_NUMBER_START_PATTERN = /* @__PURE__ */ new RegExp("[" + _constants.PLUS_CHARS + _constants.VALID_DIGITS + "]");
	var AFTER_PHONE_NUMBER_END_PATTERN = /* @__PURE__ */ new RegExp("[^" + _constants.VALID_DIGITS + "#]+$");
	var USE_NON_GEOGRAPHIC_COUNTRY_CODE = false;
	/**
	* Parses a phone number.
	*
	* parse('123456789', { defaultCountry: 'RU', v2: true }, metadata)
	* parse('123456789', { defaultCountry: 'RU' }, metadata)
	* parse('123456789', undefined, metadata)
	*
	* @param  {string} input
	* @param  {object} [options]
	* @param  {object} metadata
	* @return {object|PhoneNumber?} If `options.v2: true` flag is passed, it returns a `PhoneNumber?` instance. Otherwise, returns an object of shape `{ phone: '...', country: '...' }` (or just `{}` if no phone number was parsed).
	*/
	function parse(text, options, metadata) {
		options = options || {};
		metadata = new _metadata["default"](metadata);
		if (options.defaultCountry && !metadata.hasCountry(options.defaultCountry)) {
			if (options.v2) throw new _ParseError["default"]("INVALID_COUNTRY");
			throw new Error("Unknown country: ".concat(options.defaultCountry));
		}
		var _parseInput = parseInput(text, options.v2, options.extract), formattedPhoneNumber = _parseInput.number, ext = _parseInput.ext, error = _parseInput.error;
		if (!formattedPhoneNumber) {
			if (options.v2) {
				if (error === "TOO_SHORT") throw new _ParseError["default"]("TOO_SHORT");
				throw new _ParseError["default"]("NOT_A_NUMBER");
			}
			return {};
		}
		var _parsePhoneNumber = parsePhoneNumber(formattedPhoneNumber, options.defaultCountry, options.defaultCallingCode, metadata), country = _parsePhoneNumber.country, nationalNumber = _parsePhoneNumber.nationalNumber, countryCallingCode = _parsePhoneNumber.countryCallingCode, countryCallingCodeSource = _parsePhoneNumber.countryCallingCodeSource, carrierCode = _parsePhoneNumber.carrierCode;
		if (!metadata.hasSelectedNumberingPlan()) {
			if (options.v2) throw new _ParseError["default"]("INVALID_COUNTRY");
			return {};
		}
		if (!nationalNumber || nationalNumber.length < _constants.MIN_LENGTH_FOR_NSN) {
			/* istanbul ignore if */
			if (options.v2) throw new _ParseError["default"]("TOO_SHORT");
			return {};
		}
		if (nationalNumber.length > _constants.MAX_LENGTH_FOR_NSN) {
			if (options.v2) throw new _ParseError["default"]("TOO_LONG");
			return {};
		}
		if (options.v2) {
			var phoneNumber = new _PhoneNumber["default"](countryCallingCode, nationalNumber, metadata.metadata);
			if (country) phoneNumber.country = country;
			if (carrierCode) phoneNumber.carrierCode = carrierCode;
			if (ext) phoneNumber.ext = ext;
			phoneNumber.__countryCallingCodeSource = countryCallingCodeSource;
			return phoneNumber;
		}
		var valid = (options.extended ? metadata.hasSelectedNumberingPlan() : country) ? (0, _matchesEntirely["default"])(nationalNumber, metadata.nationalNumberPattern()) : false;
		if (!options.extended) return valid ? result(country, nationalNumber, ext) : {};
		return {
			country,
			countryCallingCode,
			carrierCode,
			valid,
			possible: valid ? true : options.extended === true && metadata.possibleLengths() && (0, _isPossible.isPossibleNumber)(nationalNumber, metadata) ? true : false,
			phone: nationalNumber,
			ext
		};
	}
	/**
	* Extracts a formatted phone number from text.
	* Doesn't guarantee that the extracted phone number
	* is a valid phone number (for example, doesn't validate its length).
	* @param  {string} text
	* @param  {boolean} [extract] — If `false`, then will parse the entire `text` as a phone number.
	* @param  {boolean} [throwOnError] — By default, it won't throw if the text is too long.
	* @return {string}
	* @example
	* // Returns "(213) 373-4253".
	* extractFormattedPhoneNumber("Call (213) 373-4253 for assistance.")
	*/
	function _extractFormattedPhoneNumber(text, extract, throwOnError) {
		if (!text) return;
		if (text.length > MAX_INPUT_STRING_LENGTH) {
			if (throwOnError) throw new _ParseError["default"]("TOO_LONG");
			return;
		}
		if (extract === false) return text;
		var startsAt = text.search(PHONE_NUMBER_START_PATTERN);
		if (startsAt < 0) return;
		return text.slice(startsAt).replace(AFTER_PHONE_NUMBER_END_PATTERN, "");
	}
	/**
	* @param  {string} text - Input.
	* @param  {boolean} v2 - Legacy API functions don't pass `v2: true` flag.
	* @param  {boolean} [extract] - Whether to extract a phone number from `text`, or attempt to parse the entire text as a phone number.
	* @return {object} `{ ?number, ?ext }`.
	*/
	function parseInput(text, v2, extract) {
		var number = (0, _extractFormattedPhoneNumberFromPossibleRfc3966NumberUri["default"])(text, { extractFormattedPhoneNumber: function extractFormattedPhoneNumber(text$1) {
			return _extractFormattedPhoneNumber(text$1, extract, v2);
		} });
		if (!number) return {};
		if (!(0, _isViablePhoneNumber["default"])(number)) {
			if ((0, _isViablePhoneNumber.isViablePhoneNumberStart)(number)) return { error: "TOO_SHORT" };
			return {};
		}
		var withExtensionStripped = (0, _extractExtension["default"])(number);
		if (withExtensionStripped.ext) return withExtensionStripped;
		return { number };
	}
	/**
	* Creates `parse()` result object.
	*/
	function result(country, nationalNumber, ext) {
		var result$1 = {
			country,
			phone: nationalNumber
		};
		if (ext) result$1.ext = ext;
		return result$1;
	}
	/**
	* Parses a viable phone number.
	* @param {string} formattedPhoneNumber — Example: "(213) 373-4253".
	* @param {string} [defaultCountry]
	* @param {string} [defaultCallingCode]
	* @param {Metadata} metadata
	* @return {object} Returns `{ country: string?, countryCallingCode: string?, nationalNumber: string? }`.
	*/
	function parsePhoneNumber(formattedPhoneNumber, defaultCountry, defaultCallingCode, metadata) {
		var _extractCountryCallin = (0, _extractCountryCallingCode["default"])((0, _parseIncompletePhoneNumber["default"])(formattedPhoneNumber), defaultCountry, defaultCallingCode, metadata.metadata), countryCallingCodeSource = _extractCountryCallin.countryCallingCodeSource, countryCallingCode = _extractCountryCallin.countryCallingCode, number = _extractCountryCallin.number;
		var country;
		if (countryCallingCode) metadata.selectNumberingPlan(countryCallingCode);
		else if (number && (defaultCountry || defaultCallingCode)) {
			metadata.selectNumberingPlan(defaultCountry, defaultCallingCode);
			if (defaultCountry) country = defaultCountry;
			else if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
				if (metadata.isNonGeographicCallingCode(defaultCallingCode)) country = "001";
			}
			countryCallingCode = defaultCallingCode || (0, _getCountryCallingCode["default"])(defaultCountry, metadata.metadata);
		} else return {};
		if (!number) return {
			countryCallingCodeSource,
			countryCallingCode
		};
		var _extractNationalNumbe = (0, _extractNationalNumber["default"])((0, _parseIncompletePhoneNumber["default"])(number), metadata), nationalNumber = _extractNationalNumbe.nationalNumber, carrierCode = _extractNationalNumbe.carrierCode;
		var exactCountry = (0, _getCountryByCallingCode["default"])(countryCallingCode, {
			nationalNumber,
			defaultCountry,
			metadata
		});
		if (exactCountry) {
			country = exactCountry;
			/* istanbul ignore if */
			if (exactCountry === "001") {} else metadata.country(country);
		}
		return {
			country,
			countryCallingCode,
			countryCallingCodeSource,
			nationalNumber,
			carrierCode
		};
	}
}) });

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