import { incrementIfAtLeast as v, noncapturingDelim as b, spliceStr as q } from "./utils-internals.js";
import { replaceUnescaped as B, Context as D } from "../../../../regex-utilities@2.3.0/node_modules/regex-utilities/src/index.js";
const E = new RegExp(String.raw`(?<noncapturingStart>${b})|(?<capturingStart>\((?:\?<[^>]+>)?)|\\?.`, "gsu");
function F(e, a) {
  const d = (a == null ? void 0 : a.hiddenCaptures) ?? [];
  let s = (a == null ? void 0 : a.captureTransfers) ?? /* @__PURE__ */ new Map();
  if (!/\(\?>/.test(e))
    return {
      pattern: e,
      captureTransfers: s,
      hiddenCaptures: d
    };
  const t = "(?>", u = "(?:(?=(", p = [0], n = [];
  let l = 0, i = 0, c = NaN, h;
  do {
    h = !1;
    let f = 0, r = 0, o = !1, g;
    for (E.lastIndex = Number.isNaN(c) ? 0 : c + u.length; g = E.exec(e); ) {
      const { 0: $, index: w, groups: { capturingStart: S, noncapturingStart: k } } = g;
      if ($ === "[")
        f++;
      else if (f)
        $ === "]" && f--;
      else if ($ === t && !o)
        c = w, o = !0;
      else if (o && k)
        r++;
      else if (S)
        o ? r++ : (l++, p.push(l + i));
      else if ($ === ")" && o) {
        if (!r) {
          i++;
          const m = l + i;
          if (e = `${e.slice(0, c)}${u}${e.slice(c + t.length, w)}))<$$${m}>)${e.slice(w + 1)}`, h = !0, n.push(m), v(d, m), s.size) {
            const G = /* @__PURE__ */ new Map();
            s.forEach((T, A) => {
              G.set(
                A >= m ? A + 1 : A,
                T.map((C) => C >= m ? C + 1 : C)
              );
            }), s = G;
          }
          break;
        }
        r--;
      }
    }
  } while (h);
  return d.push(...n), e = B(
    e,
    String.raw`\\(?<backrefNum>[1-9]\d*)|<\$\$(?<wrappedBackrefNum>\d+)>`,
    ({ 0: f, groups: { backrefNum: r, wrappedBackrefNum: o } }) => {
      if (r) {
        const g = +r;
        if (g > p.length - 1)
          throw new Error(`Backref "${f}" greater than number of captures`);
        return `\\${p[g]}`;
      }
      return `\\${o}`;
    },
    D.DEFAULT
  ), {
    pattern: e,
    captureTransfers: s,
    hiddenCaptures: d
  };
}
const N = String.raw`(?:[?*+]|\{\d+(?:,\d*)?\})`, I = new RegExp(String.raw`
\\(?: \d+
  | c[A-Za-z]
  | [gk]<[^>]+>
  | [pPu]\{[^\}]+\}
  | u[A-Fa-f\d]{4}
  | x[A-Fa-f\d]{2}
  )
| \((?: \? (?: [:=!>]
  | <(?:[=!]|[^>]+>)
  | [A-Za-z\-]+:
  | \(DEFINE\)
  ))?
| (?<qBase>${N})(?<qMod>[?+]?)(?<invalidQ>[?*+\{]?)
| \\?.
`.replace(/\s+/g, ""), "gsu");
function z(e) {
  if (!new RegExp(`${N}\\+`).test(e))
    return {
      pattern: e
    };
  const a = [];
  let d = null, s = null, t = "", u = 0, p;
  for (I.lastIndex = 0; p = I.exec(e); ) {
    const { 0: n, index: l, groups: { qBase: i, qMod: c, invalidQ: h } } = p;
    if (n === "[")
      u || (s = l), u++;
    else if (n === "]")
      u ? u-- : s = null;
    else if (!u)
      if (c === "+" && t && !t.startsWith("(")) {
        if (h)
          throw new Error(`Invalid quantifier "${n}"`);
        let f = -1;
        if (/^\{\d+\}$/.test(i))
          e = q(e, l + i.length, c, "");
        else {
          if (t === ")" || t === "]") {
            const r = t === ")" ? d : s;
            if (r === null)
              throw new Error(`Invalid unmatched "${t}"`);
            e = `${e.slice(0, r)}(?>${e.slice(r, l)}${i})${e.slice(l + n.length)}`;
          } else
            e = `${e.slice(0, l - t.length)}(?>${t}${i})${e.slice(l + n.length)}`;
          f += 4;
        }
        I.lastIndex += f;
      } else n[0] === "(" ? a.push(l) : n === ")" && (d = a.length ? a.pop() : null);
    t = n;
  }
  return {
    pattern: e
  };
}
export {
  F as atomic,
  z as possessive
};
