import { __export } from "../../../../_virtual/rolldown_runtime.js";
import { RegExpParser } from "../../../@chevrotain/regexp-to-ast/lib/src/regexp-parser.js";
import { BaseRegExpVisitor } from "../../../@chevrotain/regexp-to-ast/lib/src/base-regexp-visitor.js";
import "../../../@chevrotain/regexp-to-ast/lib/src/api.js";
var regexp_utils_exports = /* @__PURE__ */ __export({
	NEWLINE_REGEXP: () => NEWLINE_REGEXP,
	escapeRegExp: () => escapeRegExp,
	getTerminalParts: () => getTerminalParts,
	isMultilineComment: () => isMultilineComment,
	isWhitespace: () => isWhitespace,
	partialMatches: () => partialMatches,
	partialRegExp: () => partialRegExp,
	whitespaceCharacters: () => whitespaceCharacters
}, 1);
const NEWLINE_REGEXP = /\r?\n/gm;
var regexpParser = new RegExpParser(), visitor = new class extends BaseRegExpVisitor {
	constructor() {
		super(...arguments), this.isStarting = !0, this.endRegexpStack = [], this.multiline = !1;
	}
	get endRegex() {
		return this.endRegexpStack.join("");
	}
	reset(e) {
		this.multiline = !1, this.regex = e, this.startRegexp = "", this.isStarting = !0, this.endRegexpStack = [];
	}
	visitGroup(e) {
		e.quantifier && (this.isStarting = !1, this.endRegexpStack = []);
	}
	visitCharacter(e) {
		let s = String.fromCharCode(e.value);
		if (!this.multiline && s === "\n" && (this.multiline = !0), e.quantifier) this.isStarting = !1, this.endRegexpStack = [];
		else {
			let e = escapeRegExp(s);
			this.endRegexpStack.push(e), this.isStarting && (this.startRegexp += e);
		}
	}
	visitSet(e) {
		if (!this.multiline) {
			let s = this.regex.substring(e.loc.begin, e.loc.end), c = new RegExp(s);
			this.multiline = !!"\n".match(c);
		}
		if (e.quantifier) this.isStarting = !1, this.endRegexpStack = [];
		else {
			let s = this.regex.substring(e.loc.begin, e.loc.end);
			this.endRegexpStack.push(s), this.isStarting && (this.startRegexp += s);
		}
	}
	visitChildren(e) {
		e.type === "Group" && e.quantifier || super.visitChildren(e);
	}
}();
function getTerminalParts(e) {
	try {
		typeof e != "string" && (e = e.source), e = `/${e}/`;
		let s = regexpParser.pattern(e), c = [];
		for (let l of s.value.value) visitor.reset(e), visitor.visit(l), c.push({
			start: visitor.startRegexp,
			end: visitor.endRegex
		});
		return c;
	} catch {
		return [];
	}
}
function isMultilineComment(e) {
	try {
		return typeof e == "string" && (e = new RegExp(e)), e = e.toString(), visitor.reset(e), visitor.visit(regexpParser.pattern(e)), visitor.multiline;
	} catch {
		return !1;
	}
}
const whitespaceCharacters = "\f\n\r	\v \xA0            \u2028\u2029  　﻿".split("");
function isWhitespace(e) {
	let s = typeof e == "string" ? new RegExp(e) : e;
	return whitespaceCharacters.some((e) => s.test(e));
}
function escapeRegExp(e) {
	return e.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function partialMatches(e, s) {
	let c = partialRegExp(e), l = s.match(c);
	return !!l && l[0].length > 0;
}
function partialRegExp(e) {
	typeof e == "string" && (e = new RegExp(e));
	let s = e, c = e.source, l = 0;
	function u() {
		let e = "", d;
		function f(s) {
			e += c.substr(l, s), l += s;
		}
		function p(s) {
			e += "(?:" + c.substr(l, s) + "|$)", l += s;
		}
		for (; l < c.length;) switch (c[l]) {
			case "\\":
				switch (c[l + 1]) {
					case "c":
						p(3);
						break;
					case "x":
						p(4);
						break;
					case "u":
						s.unicode ? c[l + 2] === "{" ? p(c.indexOf("}", l) - l + 1) : p(6) : p(2);
						break;
					case "p":
					case "P":
						s.unicode ? p(c.indexOf("}", l) - l + 1) : p(2);
						break;
					case "k":
						p(c.indexOf(">", l) - l + 1);
						break;
					default:
						p(2);
						break;
				}
				break;
			case "[":
				d = /\[(?:\\.|.)*?\]/g, d.lastIndex = l, d = d.exec(c) || [], p(d[0].length);
				break;
			case "|":
			case "^":
			case "$":
			case "*":
			case "+":
			case "?":
				f(1);
				break;
			case "{":
				d = /\{\d+,?\d*\}/g, d.lastIndex = l, d = d.exec(c), d ? f(d[0].length) : p(1);
				break;
			case "(":
				if (c[l + 1] === "?") switch (c[l + 2]) {
					case ":":
						e += "(?:", l += 3, e += u() + "|$)";
						break;
					case "=":
						e += "(?=", l += 3, e += u() + ")";
						break;
					case "!":
						d = l, l += 3, u(), e += c.substr(d, l - d);
						break;
					case "<":
						switch (c[l + 3]) {
							case "=":
							case "!":
								d = l, l += 4, u(), e += c.substr(d, l - d);
								break;
							default:
								f(c.indexOf(">", l) - l + 1), e += u() + "|$)";
								break;
						}
						break;
				}
				else f(1), e += u() + "|$)";
				break;
			case ")": return ++l, e;
			default:
				p(1);
				break;
		}
		return e;
	}
	return new RegExp(u(), e.flags);
}
export { NEWLINE_REGEXP, escapeRegExp, isMultilineComment, isWhitespace, partialMatches, regexp_utils_exports };
