import { hasUnescaped as x, Context as R, getGroupContents as H, forEachUnescaped as W, replaceUnescaped as _ } from "../../../../regex-utilities@2.3.0/node_modules/regex-utilities/src/index.js";
const d = String.raw, j = d`\\g<(?<gRNameOrNum>[^>&]+)&R=(?<gRDepth>[^>]+)>`, U = d`\(\?R=(?<rDepth>[^\)]+)\)|${j}`, T = d`\(\?<(?![=!])(?<captureName>[^>]+)>`, G = d`${T}|(?<unnamed>\()(?!\?)`, C = new RegExp(d`${T}|${U}|\(\?|\\?.`, "gsu"), L = "Cannot use multiple overlapping recursions";
function J(e, t) {
  const { hiddenCaptures: n, mode: r } = {
    hiddenCaptures: [],
    mode: "plugin",
    ...t
  };
  let a = (t == null ? void 0 : t.captureTransfers) ?? /* @__PURE__ */ new Map();
  if (!new RegExp(U, "su").test(e))
    return {
      pattern: e,
      captureTransfers: a,
      hiddenCaptures: n
    };
  if (r === "plugin" && x(e, d`\(\?\(DEFINE\)`, R.DEFAULT))
    throw new Error("DEFINE groups cannot be used with recursion");
  const f = [], o = x(e, d`\\[1-9]`, R.DEFAULT), l = /* @__PURE__ */ new Map(), h = [];
  let w = !1, i = 0, u = 0, m;
  for (C.lastIndex = 0; m = C.exec(e); ) {
    const { 0: g, groups: { captureName: p, rDepth: c, gRNameOrNum: s, gRDepth: D } } = m;
    if (g === "[")
      i++;
    else if (i)
      g === "]" && i--;
    else if (c) {
      if (v(c), w)
        throw new Error(L);
      if (o)
        throw new Error(
          // When used in `external` mode by transpilers other than Regex+, backrefs might have
          // gone through conversion from named to numbered, so avoid a misleading error
          `${r === "external" ? "Backrefs" : "Numbered backrefs"} cannot be used with global recursion`
        );
      const $ = e.slice(0, m.index), E = e.slice(C.lastIndex);
      if (x(E, U, R.DEFAULT))
        throw new Error(L);
      const N = +c - 1;
      e = B(
        $,
        E,
        N,
        !1,
        n,
        f,
        u
      ), a = S(
        a,
        $,
        N,
        f.length,
        0,
        u
      );
      break;
    } else if (s) {
      v(D);
      let $ = !1;
      for (const b of h)
        if (b.name === s || b.num === +s) {
          if ($ = !0, b.hasRecursedWithin)
            throw new Error(L);
          break;
        }
      if (!$)
        throw new Error(d`Recursive \g cannot be used outside the referenced group "${r === "external" ? s : d`\g<${s}&R=${D}>`}"`);
      const E = l.get(s), N = H(e, E);
      if (o && x(N, d`${T}|\((?!\?)`, R.DEFAULT))
        throw new Error(
          // When used in `external` mode by transpilers other than Regex+, backrefs might have
          // gone through conversion from named to numbered, so avoid a misleading error
          `${r === "external" ? "Backrefs" : "Numbered backrefs"} cannot be used with recursion of capturing groups`
        );
      const k = e.slice(E, m.index), F = N.slice(k.length + g.length), I = f.length, A = +D - 1, M = B(
        k,
        F,
        A,
        !0,
        n,
        f,
        u
      );
      a = S(
        a,
        k,
        A,
        f.length - I,
        I,
        u
      );
      const O = e.slice(0, E), z = e.slice(E + N.length);
      e = `${O}${M}${z}`, C.lastIndex += M.length - g.length - k.length - F.length, h.forEach((b) => b.hasRecursedWithin = !0), w = !0;
    } else if (p)
      u++, l.set(String(u), C.lastIndex), l.set(p, C.lastIndex), h.push({
        num: u,
        name: p
      });
    else if (g[0] === "(") {
      const $ = g === "(";
      $ && (u++, l.set(String(u), C.lastIndex)), h.push($ ? { num: u } : {});
    } else g === ")" && h.pop();
  }
  return n.push(...f), {
    pattern: e,
    captureTransfers: a,
    hiddenCaptures: n
  };
}
function v(e) {
  const t = `Max depth must be integer between 2 and 100; used ${e}`;
  if (!/^[1-9]\d*$/.test(e))
    throw new Error(t);
  if (e = +e, e < 2 || e > 100)
    throw new Error(t);
}
function B(e, t, n, r, a, f, o) {
  const l = /* @__PURE__ */ new Set();
  r && W(e + t, T, ({ groups: { captureName: w } }) => {
    l.add(w);
  }, R.DEFAULT);
  const h = [
    n,
    r ? l : null,
    a,
    f,
    o
  ];
  return `${e}${P(`(?:${e}`, "forward", ...h)}(?:)${P(`${t})`, "backward", ...h)}${t}`;
}
function P(e, t, n, r, a, f, o) {
  const h = (i) => t === "forward" ? i + 2 : n - i + 2 - 1;
  let w = "";
  for (let i = 0; i < n; i++) {
    const u = h(i);
    w += _(
      e,
      d`${G}|\\k<(?<backref>[^>]+)>`,
      ({ 0: m, groups: { captureName: g, unnamed: p, backref: c } }) => {
        if (c && r && !r.has(c))
          return m;
        const s = `_$${u}`;
        if (p || g) {
          const D = o + f.length + 1;
          return f.push(D), q(a, D), p ? m : `(?<${g}${s}>`;
        }
        return d`\k<${c}${s}>`;
      },
      R.DEFAULT
    );
  }
  return w;
}
function q(e, t) {
  for (let n = 0; n < e.length; n++)
    e[n] >= t && e[n]++;
}
function S(e, t, n, r, a, f) {
  if (e.size && r) {
    let o = 0;
    W(t, G, () => o++, R.DEFAULT);
    const l = f - o + a, h = /* @__PURE__ */ new Map();
    return e.forEach((w, i) => {
      const u = (r - o * n) / n, m = o * n, g = i > l + o ? i + r : i, p = [];
      for (const c of w)
        if (c <= l)
          p.push(c);
        else if (c > l + o + u)
          p.push(c + r);
        else if (c <= l + o)
          for (let s = 0; s <= n; s++)
            p.push(c + o * s);
        else
          for (let s = 0; s <= n; s++)
            p.push(c + m + u * s);
      h.set(g, p);
    }), h;
  }
  return e;
}
export {
  J as recursion
};
