import { CONFIG_VALIDATION } from "../constants/error-messages.js";
import { re2 } from "../expose.js";
import { getEnv } from "./env.js";
import { isRegExp } from "@sindresorhus/is";

//#region lib/util/regex.ts
const cache = /* @__PURE__ */ new Map();
let status;
let RegEx = RegExp;
/* v8 ignore next 2 */
if (getEnv().RENOVATE_X_IGNORE_RE2) status = { type: "ignored" };
else try {
	const RE2 = re2();
	new RE2(".*").exec("test");
	RegEx = RE2;
	status = { type: "available" };
} catch (err) {
	status = {
		type: "unavailable",
		err
	};
}
const regexEngineStatus = status;
function regEx(pattern, flags, useCache = true) {
	let canBeCached = useCache;
	if (canBeCached && flags?.includes("g")) canBeCached = false;
	if (canBeCached && isRegExp(pattern) && pattern.flags.includes("g")) canBeCached = false;
	const key = flags ? `${pattern.toString()}:${flags}` : pattern.toString();
	if (canBeCached) {
		const cachedResult = cache.get(key);
		if (cachedResult) return cachedResult;
	}
	try {
		const instance = flags ? new RegEx(pattern, flags) : new RegEx(pattern);
		if (canBeCached) cache.set(key, instance);
		return instance;
	} catch (err) {
		const error = new Error(CONFIG_VALIDATION);
		error.validationMessage = err.message;
		error.validationSource = pattern.toString();
		error.validationError = `Invalid regular expression (re2): ${pattern.toString()}`;
		throw error;
	}
}
function escapeRegExp(input) {
	return input.replace(regEx(/[.*+\-?^${}()|[\]\\]/g), "\\$&");
}
const newlineRegex = regEx(/\r?\n/);

//#endregion
export { escapeRegExp, newlineRegex, regEx, regexEngineStatus };
//# sourceMappingURL=regex.js.map