import { useMemo as d, useEffect as x } from "react";
const m = "search-result-highlight", u = () => typeof window < "u" && "CSS" in window && "highlights" in CSS && typeof Highlight < "u", w = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), H = (s, c, h) => {
  const t = [];
  if (!c)
    return t;
  const n = new RegExp(w(c), h ? "g" : "gi"), a = document.createTreeWalker(s, NodeFilter.SHOW_TEXT);
  let e = a.nextNode();
  for (; e; ) {
    const o = e.textContent ?? "";
    n.lastIndex = 0;
    let r = n.exec(o);
    for (; r; ) {
      const i = document.createRange();
      i.setStart(e, r.index), i.setEnd(e, r.index + r[0].length), t.push(i), r = n.exec(o);
    }
    e = a.nextNode();
  }
  return t;
}, C = (s) => {
  const {
    ref: c,
    query: h,
    highlightClassName: t = m,
    scopeSelector: n,
    caseSensitive: a = !1,
    deps: e
  } = s, o = d(() => h.trim(), [h]), r = d(() => e ?? [], [e]);
  x(() => {
    if (!u())
      return;
    const i = c.current;
    if (!i)
      return;
    const g = CSS.highlights.get(t) ?? new Highlight();
    CSS.highlights.set(t, g);
    const p = () => {
      if (g.clear(), !o)
        return;
      const l = n ? i.querySelector(n) : i;
      if (l)
        for (const f of H(l, o, a))
          g.add(f);
    }, S = window.requestAnimationFrame(p);
    return () => {
      window.cancelAnimationFrame(S), g.clear();
    };
  }, [a, t, o, c, n, r]);
};
C.supported = () => u();
export {
  m as SEARCH_HIGHLIGHT_CLASS,
  C as default
};
//# sourceMappingURL=useSearchHighlight.js.map
