import { useState as l, useCallback as x, useEffect as C } from "react";
import { getHighlightOptionsWithDefaults as H, findMatches as M } from "../highlight.js";
const d = (i) => {
  const [n, a] = l(void 0), [u, r] = l([]), f = x(async (t) => {
    if (!t)
      return {};
    const s = t.numPages, e = {};
    for (let o = 1; o <= s; o++) {
      const g = await t.getPage(o);
      e[o.toString()] = await g.getTextContent();
    }
    return e;
  }, []), m = async (t) => {
    a(t);
  }, c = () => {
    r([]), a(void 0);
  };
  return C(() => {
    i && f(i).then((t) => {
      let s = [];
      for (const { keyword: e, highlightColor: o, options: g } of n) {
        if (!e)
          continue;
        const p = H(g);
        for (const h of Object.keys(t)) {
          const y = M(
            [e],
            t[h],
            Number(h) - 1,
            p
          ).map((b, w) => ({
            ...b,
            page: Number(h),
            pageMatchIdx: w,
            color: o,
            keyword: e
          }));
          s = [...s, ...y];
        }
      }
      r(s);
    }).catch(() => {
      c();
    });
  }, [n, i]), {
    highlight: m,
    clear: c,
    highlightMatches: u,
    highlightKeywords: n
  };
};
export {
  d as useHighlight
};
