const require_rolldown_runtime = require('../../../../../../_virtual/rolldown_runtime.js');

//#region ../../node_modules/.pnpm/libphonenumber-js@1.11.3/node_modules/libphonenumber-js/build/AsYouTypeFormatter.PatternParser.js
var require_AsYouTypeFormatter_PatternParser = /* @__PURE__ */ require_rolldown_runtime.__commonJS({ "../../node_modules/.pnpm/libphonenumber-js@1.11.3/node_modules/libphonenumber-js/build/AsYouTypeFormatter.PatternParser.js": ((exports) => {
	Object.defineProperty(exports, "__esModule", { value: true });
	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 PatternParser = /* @__PURE__ */ function() {
		function PatternParser$1() {
			_classCallCheck(this, PatternParser$1);
		}
		_createClass(PatternParser$1, [
			{
				key: "parse",
				value: function parse(pattern) {
					this.context = [{
						or: true,
						instructions: []
					}];
					this.parsePattern(pattern);
					if (this.context.length !== 1) throw new Error("Non-finalized contexts left when pattern parse ended");
					var _this$context$ = this.context[0], branches = _this$context$.branches, instructions = _this$context$.instructions;
					if (branches) return {
						op: "|",
						args: branches.concat([expandSingleElementArray(instructions)])
					};
					/* istanbul ignore if */
					if (instructions.length === 0) throw new Error("Pattern is required");
					if (instructions.length === 1) return instructions[0];
					return instructions;
				}
			},
			{
				key: "startContext",
				value: function startContext(context) {
					this.context.push(context);
				}
			},
			{
				key: "endContext",
				value: function endContext() {
					this.context.pop();
				}
			},
			{
				key: "getContext",
				value: function getContext() {
					return this.context[this.context.length - 1];
				}
			},
			{
				key: "parsePattern",
				value: function parsePattern(pattern) {
					if (!pattern) throw new Error("Pattern is required");
					var match = pattern.match(OPERATOR);
					if (!match) {
						if (ILLEGAL_CHARACTER_REGEXP.test(pattern)) throw new Error("Illegal characters found in a pattern: ".concat(pattern));
						this.getContext().instructions = this.getContext().instructions.concat(pattern.split(""));
						return;
					}
					var operator = match[1];
					var before = pattern.slice(0, match.index);
					var rightPart = pattern.slice(match.index + operator.length);
					switch (operator) {
						case "(?:":
							if (before) this.parsePattern(before);
							this.startContext({
								or: true,
								instructions: [],
								branches: []
							});
							break;
						case ")":
							if (!this.getContext().or) throw new Error("\")\" operator must be preceded by \"(?:\" operator");
							if (before) this.parsePattern(before);
							if (this.getContext().instructions.length === 0) throw new Error("No instructions found after \"|\" operator in an \"or\" group");
							var branches = this.getContext().branches;
							branches.push(expandSingleElementArray(this.getContext().instructions));
							this.endContext();
							this.getContext().instructions.push({
								op: "|",
								args: branches
							});
							break;
						case "|":
							if (!this.getContext().or) throw new Error("\"|\" operator can only be used inside \"or\" groups");
							if (before) this.parsePattern(before);
							if (!this.getContext().branches)
 /* istanbul ignore else */
							if (this.context.length === 1) this.getContext().branches = [];
							else throw new Error("\"branches\" not found in an \"or\" group context");
							this.getContext().branches.push(expandSingleElementArray(this.getContext().instructions));
							this.getContext().instructions = [];
							break;
						case "[":
							if (before) this.parsePattern(before);
							this.startContext({ oneOfSet: true });
							break;
						case "]":
							if (!this.getContext().oneOfSet) throw new Error("\"]\" operator must be preceded by \"[\" operator");
							this.endContext();
							this.getContext().instructions.push({
								op: "[]",
								args: parseOneOfSet(before)
							});
							break;
						default: throw new Error("Unknown operator: ".concat(operator));
					}
					if (rightPart) this.parsePattern(rightPart);
				}
			}
		]);
		return PatternParser$1;
	}();
	exports["default"] = PatternParser;
	function parseOneOfSet(pattern) {
		var values = [];
		var i = 0;
		while (i < pattern.length) {
			if (pattern[i] === "-") {
				if (i === 0 || i === pattern.length - 1) throw new Error("Couldn't parse a one-of set pattern: ".concat(pattern));
				var prevValue = pattern[i - 1].charCodeAt(0) + 1;
				var nextValue = pattern[i + 1].charCodeAt(0) - 1;
				var value = prevValue;
				while (value <= nextValue) {
					values.push(String.fromCharCode(value));
					value++;
				}
			} else values.push(pattern[i]);
			i++;
		}
		return values;
	}
	var ILLEGAL_CHARACTER_REGEXP = /[\(\)\[\]\?\:\|]/;
	var OPERATOR = /* @__PURE__ */ new RegExp("(\\||\\(\\?\\:|\\)|\\[|\\])");
	function expandSingleElementArray(array) {
		if (array.length === 1) return array[0];
		return array;
	}
}) });

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