import { hidePiiData as n } from "../hidePiiData.js";
const m = "shuffle", I = "mask", P = "reset", d = "data-pii", l = "data-original-pii", a = "data-hide-pii", A = "data-obfuscated-pii", O = '[data-pii="visible"]', h = '[data-pii="hidden"]', g = "[data-obfuscated-pii]", E = "[data-original-pii]", N = () => {
  const r = document.documentElement;
  let c = null, f = !1;
  const y = () => {
    const i = r.querySelectorAll(O);
    for (const t of i)
      n(t);
  }, D = () => {
    const i = r.querySelectorAll(h + E);
    for (const t of i) {
      const e = t.getAttribute(l);
      n(t, String(e)), t.removeAttribute(l), t.removeAttribute(A), t.setAttribute(d, "visible");
    }
  }, T = () => {
    c || (c = new MutationObserver((i) => {
      const t = r.getAttribute(a);
      if (!(t !== m && t !== I)) {
        for (const e of i)
          if (e.type === "childList") {
            for (const s of e.addedNodes)
              if (s.nodeType === Node.ELEMENT_NODE) {
                const o = s;
                o.matches(O) && n(o);
                const u = o.querySelectorAll(O);
                for (const b of u)
                  n(b);
              }
          } else if (e.type === "attributes" && e.attributeName === d) {
            const s = e.target;
            s.getAttribute(d) === "visible" && n(s);
          }
      }
    }), c.observe(document.body, {
      subtree: !0,
      childList: !0,
      attributes: !0,
      attributeFilter: [d]
    }));
  }, _ = () => {
    c && (c.disconnect(), c = null);
  };
  new MutationObserver((i) => {
    const t = (e) => {
      const s = e.getAttribute(d) === "hidden", o = e.getAttribute(A), u = e.getAttribute(l);
      if (!o || !u || !s)
        return;
      const b = e instanceof HTMLInputElement ? e.value ?? "" : e.textContent ?? "";
      (b === u || b !== o) && n(e, o);
    };
    for (const e of i) {
      if (e.type === "childList") {
        for (const s of e.addedNodes)
          if (s instanceof Element) {
            t(s);
            const o = s.querySelectorAll(g + E);
            for (const u of o)
              t(u);
          }
      }
      if (e.type === "attributes") {
        const s = e.target;
        t(s);
      }
      if (e.type === "characterData") {
        const s = e.target.parentElement;
        s?.matches(h + g + E) && t(s);
      }
    }
    p();
  }).observe(document.body, {
    subtree: !0,
    childList: !0,
    attributes: !0,
    characterData: !0,
    attributeFilter: [A, l, d, "value"]
  });
  const p = () => {
    const i = document.body.querySelectorAll(
      g + E
    );
    for (const t of i) {
      const e = t.getAttribute(l), s = t.getAttribute(A);
      L(t) === e && n(t, String(s));
    }
  }, v = () => {
    const i = r.getAttribute(a), t = i === m, e = i === P;
    !t && f && (_(), f = !1), e && D(), t && !f && (T(), f = !0, y(), p());
  };
  new MutationObserver(v).observe(r, { attributes: !0, attributeFilter: [a] }), v();
  const L = (i) => i instanceof HTMLInputElement ? i.value : i.textContent ?? "", S = (i) => {
    if ((i.ctrlKey || i.metaKey) && i.shiftKey && i.key.toLowerCase() === "u") {
      const t = r.getAttribute(a);
      t === m ? r.setAttribute(a, I) : t === I ? r.setAttribute(a, P) : r.setAttribute(a, m);
    }
  };
  document.addEventListener("keydown", S);
};
export {
  N as initPiiObserver
};
//# sourceMappingURL=initPiiObserver.js.map
