/** Convert a simple glob pattern to a RegExp.
 *  Supports `*` (any chars except /), `**` (any chars including /), and `?` (single char). */
export function globToRegex(pattern: string): RegExp {
	let re = "";
	let i = 0;
	while (i < pattern.length) {
		const ch = pattern[i];
		if (ch === "*" && pattern[i + 1] === "*") {
			re += ".*";
			i += 2;
			if (pattern[i] === "/") i++; // skip separator after **
		} else if (ch === "*") {
			re += "[^/]*";
			i++;
		} else if (ch === "?") {
			re += "[^/]";
			i++;
		} else if (".+^{}()$|[]\\".includes(ch)) {
			re += "\\" + ch;
			i++;
		} else {
			re += ch;
			i++;
		}
	}
	return new RegExp(re, "i");
}
