"use client";
import b from "../node_modules/escape-string-regexp/index.js";
import { convert as E } from "../../unist-util-is/lib/index.js";
import { visitParents as I } from "../../unist-util-visit-parents/lib/index.js";
function q(e, f, a) {
  const p = E((a || {}).ignore || []), x = R(f);
  let d = -1;
  for (; ++d < x.length; )
    I(e, "text", m);
  function m(n, s) {
    let c = -1, t;
    for (; ++c < s.length; ) {
      const u = s[c], r = t ? t.children : void 0;
      if (p(
        u,
        r ? r.indexOf(u) : void 0,
        t
      ))
        return;
      t = u;
    }
    if (t)
      return w(n, s);
  }
  function w(n, s) {
    const c = s[s.length - 1], t = x[d][0], u = x[d][1];
    let r = 0;
    const y = c.children.indexOf(n);
    let v = !1, l = [];
    t.lastIndex = 0;
    let o = t.exec(n.value);
    for (; o; ) {
      const h = o.index, A = {
        index: o.index,
        input: o.input,
        stack: [...s, n]
      };
      let i = u(...o, A);
      if (typeof i == "string" && (i = i.length > 0 ? { type: "text", value: i } : void 0), i === !1 ? t.lastIndex = h + 1 : (r !== h && l.push({
        type: "text",
        value: n.value.slice(r, h)
      }), Array.isArray(i) ? l.push(...i) : i && l.push(i), r = h + o[0].length, v = !0), !t.global)
        break;
      o = t.exec(n.value);
    }
    return v ? (r < n.value.length && l.push({ type: "text", value: n.value.slice(r) }), c.children.splice(y, 1, ...l)) : l = [n], y + l.length;
  }
}
function R(e) {
  const f = [];
  if (!Array.isArray(e))
    throw new TypeError("Expected find and replace tuple or list of tuples");
  const a = !e[0] || Array.isArray(e[0]) ? e : [e];
  let g = -1;
  for (; ++g < a.length; ) {
    const p = a[g];
    f.push([k(p[0]), P(p[1])]);
  }
  return f;
}
function k(e) {
  return typeof e == "string" ? new RegExp(b(e), "g") : e;
}
function P(e) {
  return typeof e == "function" ? e : function() {
    return e;
  };
}
export {
  q as findAndReplace
};
