const S = "data-pii", A = "data-original-pii", x = "data-obfuscated-pii", T = (e) => {
  for (let n = e.length - 1; n > 0; n--) {
    const s = Math.floor(Math.random() * (n + 1)), t = e[n];
    e[n] = e[s], e[s] = t;
  }
}, D = (e) => /[A-Za-z0-9]/.test(e), b = (e) => {
  const n = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  let s = "";
  for (let t = 0; t < e; t++)
    s += n.charAt(Math.floor(Math.random() * n.length)).toLocaleLowerCase();
  return s;
}, w = (e, n = 6) => e.length >= n ? e : e + b(n - e.length), _ = (e) => {
  const n = "0123456789";
  let s = "";
  for (let t = 0; t < e; t++)
    s += n.charAt(Math.floor(Math.random() * n.length));
  return s;
}, I = (e, n = 6) => e.length >= n ? e : e + _(n - e.length), P = (e) => {
  const n = e.length, s = e.split(""), t = [], r = [];
  for (const i of s)
    D(i) ? t.push(i) : r.push(i);
  if (t.length === 0 || n >= 2 && t.length === 1)
    return e;
  T(t), T(r);
  const h = Math.max(0, n - 2);
  if (r.length > h)
    return e;
  const a = new Array(n);
  a[0] = t.pop(), a[n - 1] = t.pop();
  const l = [];
  for (let i = 1; i <= n - 2; i++)
    l.push(i);
  T(l);
  let f = 0;
  for (const i of r)
    a[l[f++]] = i;
  for (const i of t)
    a[l[f++]] = i;
  return a.join("");
}, M = (e) => {
  const [n, s] = e.split("@"), t = n.replace(/[^A-Za-z0-9]/g, ""), r = s.replace(/[^A-Za-z0-9]/g, ""), h = w(t, 6), a = N(h, !1), l = N(r, !1);
  return `${a}@${l}.${(() => {
    try {
      const p = (typeof navigator < "u" && (navigator.languages?.[0] || navigator.language) || "" || "").replace("_", "-");
      if (!p)
        return "com";
      const o = p.split("-");
      return (o[1] || o[0] || "").slice(0, 2).toLocaleLowerCase() || "com";
    } catch {
      return "com";
    }
  })()}`;
}, C = (e) => {
  const n = e.startsWith("+") && e.slice(1).match(/^\d{1,3}/)?.[0] || "", s = e.replace(/\D/g, ""), t = n ? s.slice(n.length) : s;
  if (t.length < 4)
    return e;
  const r = I(t, 6), h = P(r);
  let a = 0, l = n.length, f = !!n;
  return e.replace(/\d/g, (i) => {
    if (f) {
      if (l > 0)
        return l -= 1, l === 0 && (f = !1), i;
      f = !1;
    }
    return h[a++] || "0";
  });
}, N = (e, n = !0) => {
  if (e.length < 2)
    return e;
  const s = e.replace(/[^A-Za-z]/g, ""), t = w(s, 6), r = P(t);
  return n ? r.charAt(0).toUpperCase() + r.slice(1).toLowerCase() : r;
}, y = (e) => {
  const n = /(^|[^A-Za-z0-9])(\+?\d(?:[ \t\-()]*\d){6,})(?=$|[^A-Za-z0-9])/g;
  return e.replace(
    n,
    (t, r, h) => r + C(h)
  ).replace(/\S+/g, (t) => {
    if (/^[^@]+@[^@]+$/.test(t))
      return M(t);
    if (/^\+?[0-9][0-9\s\-()]{3,}$/.test(t))
      return C(t);
    if (!/[A-Za-z]/.test(t))
      return t;
    const r = t.length < 6 ? t + b(6 - t.length) : t;
    return N(r);
  });
}, v = (e, n) => {
  const s = e instanceof Element ? [e] : Array.from(e);
  for (const t of s) {
    t.setAttribute(S, "hidden");
    const r = (t.textContent || "").trim();
    if (t.matches?.("input") || t.tagName === "INPUT") {
      const o = t, c = o.value || "";
      if (typeof n == "string" && n.length > 0) {
        t.setAttribute(A, c), o.value = n, t.setAttribute(x, o.value);
        continue;
      }
      if (t.setAttribute(A, c), c.trim().length > 0) {
        const d = y(c);
        d !== c && (o.value = d), t.setAttribute(x, o.value);
      }
      continue;
    }
    const h = (o, c) => {
      if (o.children.length === 1) {
        const g = o.children[0];
        if (g && g.children.length === 0) {
          g.textContent = c;
          return;
        }
      }
      const u = document.createTreeWalker(o, NodeFilter.SHOW_TEXT, null), d = [];
      let m = u.nextNode();
      for (; m; ) {
        const g = m;
        (g.nodeValue || "").trim().length > 0 && d.push(g), m = u.nextNode();
      }
      if (d.length === 0) {
        o.textContent = c;
        return;
      }
      d[0].nodeValue = c;
      for (let g = 1; g < d.length; g++)
        d[g].nodeValue = "";
    };
    if (typeof n == "string" && n.length > 0) {
      r && t.setAttribute(A, r), h(t, n), t.setAttribute(x, (t.textContent || "").trim());
      continue;
    }
    let a = !1;
    const l = document.createTreeWalker(t, NodeFilter.SHOW_TEXT, null);
    let f = l.nextNode();
    for (; f; ) {
      const o = f, c = String(o.textContent || "");
      if (/^\s*$/.test(c)) {
        const m = l.nextNode();
        o.remove(), f = m;
        continue;
      }
      const u = c.replace(/\s+/g, " ");
      if (u.trim().length > 0) {
        const m = y(u);
        m !== u && (o.textContent = m, a = !0);
      }
      f = l.nextNode();
    }
    const i = Array.from(t.childNodes).filter((o) => o.nodeType === Node.TEXT_NODE);
    for (let o = 0; o < i.length - 1; o++) {
      const c = i[o], u = i[o + 1];
      if (c.textContent?.trim() && u.textContent?.trim()) {
        const d = document.createTextNode(" ");
        c.parentNode?.insertBefore(d, u);
      }
    }
    const p = (t.textContent || "").trim();
    a && p && (t.setAttribute(x, p), r && t.setAttribute("data-original-pii", r));
  }
};
export {
  v as hidePiiData,
  y as shufflePii,
  N as shuffleText
};
//# sourceMappingURL=hidePiiData.js.map
