const a = Object.freeze({
  DEFAULT: "DEFAULT",
  CHAR_CLASS: "CHAR_CLASS"
});
function p(t, i, s, c) {
  const u = new RegExp(String.raw`${i}|(?<$skip>\[\^?|\\?.)`, "gsu"), n = [!1];
  let e = 0, l = "";
  for (const f of t.matchAll(u)) {
    const { 0: o, groups: { $skip: r } } = f;
    if (!r && (!c || c === a.DEFAULT == !e)) {
      s instanceof Function ? l += s(f, {
        context: e ? a.CHAR_CLASS : a.DEFAULT,
        negated: n[n.length - 1]
      }) : l += s;
      continue;
    }
    o[0] === "[" ? (e++, n.push(o[1] === "^")) : o === "]" && e && (e--, n.pop()), l += o;
  }
  return l;
}
function h(t, i, s, c) {
  p(t, i, s, c);
}
function g(t, i, s = 0, c) {
  if (!new RegExp(i, "su").test(t))
    return null;
  const u = new RegExp(`${i}|(?<$skip>\\\\?.)`, "gsu");
  u.lastIndex = s;
  let n = 0, e;
  for (; e = u.exec(t); ) {
    const { 0: l, groups: { $skip: f } } = e;
    if (!f && (!c || c === a.DEFAULT == !n))
      return e;
    l === "[" ? n++ : l === "]" && n && n--, u.lastIndex == e.index && u.lastIndex++;
  }
  return null;
}
function C(t, i, s) {
  return !!g(t, i, 0, s);
}
function d(t, i) {
  const s = /\\?./gsu;
  s.lastIndex = i;
  let c = t.length, u = 0, n = 1, e;
  for (; e = s.exec(t); ) {
    const [l] = e;
    if (l === "[")
      u++;
    else if (u)
      l === "]" && u--;
    else if (l === "(")
      n++;
    else if (l === ")" && (n--, !n)) {
      c = e.index;
      break;
    }
  }
  return t.slice(i, c);
}
export {
  a as Context,
  g as execUnescaped,
  h as forEachUnescaped,
  d as getGroupContents,
  C as hasUnescaped,
  p as replaceUnescaped
};
