import se from "./node_modules/character-entities-legacy/index.json.mjs";
import ve from "../character-reference-invalid/index.json.mjs";
import { i as me } from "../is-decimal/index.mjs";
import { i as de } from "../is-hexadecimal/index.mjs";
import { i as ue } from "../is-alphanumerical/index.mjs";
import { d as pe } from "./decode-entity.browser.mjs";
var K = se, L = ve, xe = me, he = de, Y = ue, Ce = pe, Re = De, be = {}.hasOwnProperty, y = String.fromCharCode, we = Function.prototype, M = {
  warning: null,
  reference: null,
  text: null,
  warningContext: null,
  referenceContext: null,
  textContext: null,
  position: {},
  additional: null,
  attribute: !1,
  nonTerminated: !0
}, ye = 9, Q = 10, ge = 12, Ne = 32, V = 38, Ae = 59, Ee = 60, Te = 61, ke = 35, qe = 88, $e = 120, Fe = 65533, g = "named", _ = "hexadecimal", j = "decimal", H = {};
H[_] = 16;
H[j] = 10;
var q = {};
q[g] = Y;
q[j] = xe;
q[_] = he;
var Z = 1, ee = 2, re = 3, ae = 4, ne = 5, X = 6, ie = 7, m = {};
m[Z] = "Named character references must be terminated by a semicolon";
m[ee] = "Numeric character references must be terminated by a semicolon";
m[re] = "Named character references cannot be empty";
m[ae] = "Numeric character references cannot be empty";
m[ne] = "Named character references must be known";
m[X] = "Numeric character references cannot be disallowed";
m[ie] = "Numeric character references cannot be outside the permissible Unicode range";
function De(e, i) {
  var t = {}, b, d;
  i || (i = {});
  for (d in M)
    b = i[d], t[d] = b == null ? M[d] : b;
  return (t.position.indent || t.position.start) && (t.indent = t.position.indent || [], t.position = t.position.start), Pe(e, t);
}
function Pe(e, i) {
  var t = i.additional, b = i.nonTerminated, d = i.text, I = i.reference, O = i.warning, te = i.textContext, fe = i.referenceContext, oe = i.warningContext, $ = i.position, le = i.indent || [], F = e.length, s = 0, R = -1, u = $.column || 1, W = $.line || 1, p = "", D = [], w, P, x, f, v, a, r, o, T, S, h, N, C, c, z, A, k, l, n;
  for (typeof t == "string" && (t = t.charCodeAt(0)), A = E(), o = O ? ce : we, s--, F++; ++s < F; )
    if (v === Q && (u = le[R] || 1), v = e.charCodeAt(s), v === V) {
      if (r = e.charCodeAt(s + 1), r === ye || r === Q || r === ge || r === Ne || r === V || r === Ee || r !== r || t && r === t) {
        p += y(v), u++;
        continue;
      }
      for (C = s + 1, N = C, n = C, r === ke ? (n = ++N, r = e.charCodeAt(n), r === qe || r === $e ? (c = _, n = ++N) : c = j) : c = g, w = "", h = "", f = "", z = q[c], n--; ++n < F && (r = e.charCodeAt(n), !!z(r)); )
        f += y(r), c === g && be.call(K, f) && (w = f, h = K[f]);
      x = e.charCodeAt(n) === Ae, x && (n++, P = c === g ? Ce(f) : !1, P && (w = f, h = P)), l = 1 + n - C, !x && !b || (f ? c === g ? (x && !h ? o(ne, 1) : (w !== f && (n = N + w.length, l = 1 + n - N, x = !1), x || (T = w ? Z : re, i.attribute ? (r = e.charCodeAt(n), r === Te ? (o(T, l), h = null) : Y(r) ? h = null : o(T, l)) : o(T, l))), a = h) : (x || o(ee, l), a = parseInt(f, H[c]), Se(a) ? (o(ie, l), a = y(Fe)) : a in L ? (o(X, l), a = L[a]) : (S = "", Ue(a) && o(X, l), a > 65535 && (a -= 65536, S += y(a >>> 10 | 55296), a = 56320 | a & 1023), a = S + y(a))) : c !== g && o(ae, l)), a ? (B(), A = E(), s = n - 1, u += n - C + 1, D.push(a), k = E(), k.offset++, I && I.call(fe, a, {
        start: A,
        end: k
      }, e.slice(C - 1, n)), A = k) : (f = e.slice(C - 1, n), p += f, u += f.length, s = n - 1);
    } else
      v === 10 && (W++, R++, u = 0), v === v ? (p += y(v), u++) : B();
  return D.join("");
  function E() {
    return {
      line: W,
      column: u,
      offset: s + ($.offset || 0)
    };
  }
  function ce(G, J) {
    var U = E();
    U.column += J, U.offset += J, O.call(oe, m[G], U, G);
  }
  function B() {
    p && (D.push(p), d && d.call(te, p, {
      start: A,
      end: E()
    }), p = "");
  }
}
function Se(e) {
  return e >= 55296 && e <= 57343 || e > 1114111;
}
function Ue(e) {
  return e >= 1 && e <= 8 || e === 11 || e >= 13 && e <= 31 || e >= 127 && e <= 159 || e >= 64976 && e <= 65007 || (e & 65535) === 65535 || (e & 65535) === 65534;
}
export {
  Re as p
};
