"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
var ce = String.fromCharCode, me = Object.prototype.hasOwnProperty, Oe = /&#(\d+);|&#x([0-9a-f]+);|&(\w+);/ig, Y = {
  amp: "&",
  apos: "'",
  gt: ">",
  lt: "<",
  quot: '"'
};
Object.keys(Y).forEach(function(f) {
  Y[f.toUpperCase()] = Y[f];
});
function xe(f, O, x, v) {
  return v ? me.call(Y, v) ? Y[v] : "&" + v + ";" : ce(O || parseInt(x, 16));
}
function U(f) {
  return f.length > 3 && f.indexOf("&") !== -1 ? f.replace(Oe, xe) : f;
}
var Ce = "http://www.w3.org/2001/XMLSchema-instance", Ae = "xsi", le = "xsi:type", ge = "non-whitespace outside of root node";
function X(f) {
  return new Error(f);
}
function de(f) {
  return "missing namespace for prefix <" + f + ">";
}
function H(f) {
  return {
    get: f,
    enumerable: true
  };
}
function we(f) {
  var O = {}, x;
  for (x in f)
    O[x] = f[x];
  return O;
}
function se(f) {
  return f + "$uri";
}
function Ne(f) {
  var O = {}, x, v;
  for (x in f)
    v = f[x], O[v] = v, O[se(v)] = x;
  return O;
}
function he() {
  return { line: 0, column: 0 };
}
function ke(f) {
  throw f;
}
function Ee(f) {
  if (!this)
    return new Ee(f);
  var O = f && f.proxy, x, v, Q, $, ae = ke, B, J, K, V, C = he, D = false, T = false, Z = null, N = false, G;
  function A(t) {
    t instanceof Error || (t = X(t)), Z = t, ae(t, C);
  }
  function m(t) {
    B && (t instanceof Error || (t = X(t)), B(t, C));
  }
  this.on = function(t, h) {
    if (typeof h != "function")
      throw X("required args <name, cb>");
    switch (t) {
      case "openTag":
        v = h;
        break;
      case "text":
        x = h;
        break;
      case "closeTag":
        Q = h;
        break;
      case "error":
        ae = h;
        break;
      case "warn":
        B = h;
        break;
      case "cdata":
        $ = h;
        break;
      case "attention":
        V = h;
        break;
      case "question":
        K = h;
        break;
      case "comment":
        J = h;
        break;
      default:
        throw X("unsupported event: " + t);
    }
    return this;
  }, this.ns = function(t) {
    if (typeof t > "u" && (t = {}), typeof t != "object")
      throw X("required args <nsMap={}>");
    var h = {}, u;
    for (u in t)
      h[u] = t[u];
    return h[Ce] = Ae, T = true, G = h, this;
  }, this.parse = function(t) {
    if (typeof t != "string")
      throw X("required args <xml=string>");
    return Z = null, be(t), C = he, N = false, Z;
  }, this.stop = function() {
    N = true;
  };
  function be(t) {
    var h = T ? [] : null, u = T ? Ne(G) : null, z, L = [], pe = 0, q = false, P = false, a = 0, n = 0, l, fe, c, r, F, W, p, ee, te, ne = "", re = 0, M;
    function ie() {
      if (M !== null)
        return M;
      var S, I, d, E = T && u.xmlns, y = T && D ? [] : null, o = re, b = ne, j = b.length, ue, _, g, k, s, R = {}, oe = {}, w, e, i;
      e:
        for (; o < j; o++)
          if (w = false, e = b.charCodeAt(o), !(e === 32 || e < 14 && e > 8)) {
            for ((e < 65 || e > 122 || e > 90 && e < 97) && e !== 95 && e !== 58 && (m("illegal first char attribute name"), w = true), i = o + 1; i < j; i++)
              if (e = b.charCodeAt(i), !(e > 96 && e < 123 || e > 64 && e < 91 || e > 47 && e < 59 || e === 46 || // '.'
              e === 45 || // '-'
              e === 95)) {
                if (e === 32 || e < 14 && e > 8) {
                  m("missing attribute value"), o = i;
                  continue e;
                }
                if (e === 61)
                  break;
                m("illegal attribute name char"), w = true;
              }
            if (s = b.substring(o, i), s === "xmlns:xmlns" && (m("illegal declaration of xmlns"), w = true), e = b.charCodeAt(i + 1), e === 34)
              i = b.indexOf('"', o = i + 2), i === -1 && (i = b.indexOf("'", o), i !== -1 && (m("attribute value quote missmatch"), w = true));
            else if (e === 39)
              i = b.indexOf("'", o = i + 2), i === -1 && (i = b.indexOf('"', o), i !== -1 && (m("attribute value quote missmatch"), w = true));
            else
              for (m("missing attribute value quotes"), w = true, i = i + 1; i < j && (e = b.charCodeAt(i + 1), !(e === 32 || e < 14 && e > 8)); i++)
                ;
            for (i === -1 && (m("missing closing quotes"), i = j, w = true), w || (g = b.substring(o, i)), o = i; i + 1 < j && (e = b.charCodeAt(i + 1), !(e === 32 || e < 14 && e > 8)); i++)
              o === i && (m("illegal character after attribute end"), w = true);
            if (o = i + 1, w)
              continue e;
            if (s in oe) {
              m("attribute <" + s + "> already defined");
              continue;
            }
            if (oe[s] = true, !T) {
              R[s] = g;
              continue;
            }
            if (D) {
              if (_ = s === "xmlns" ? "xmlns" : s.charCodeAt(0) === 120 && s.substr(0, 6) === "xmlns:" ? s.substr(6) : null, _ !== null) {
                if (S = U(g), I = se(_), k = G[S], !k) {
                  if (_ === "xmlns" || I in u && u[I] !== S)
                    do
                      k = "ns" + pe++;
                    while (typeof u[k] < "u");
                  else
                    k = _;
                  G[S] = k;
                }
                u[_] !== k && (ue || (u = we(u), ue = true), u[_] = k, _ === "xmlns" && (u[se(k)] = S, E = k), u[I] = S), R[s] = g;
                continue;
              }
              y.push(s, g);
              continue;
            }
            if (e = s.indexOf(":"), e === -1) {
              R[s] = g;
              continue;
            }
            if (!(d = u[s.substring(0, e)])) {
              m(de(s.substring(0, e)));
              continue;
            }
            s = E === d ? s.substr(e + 1) : d + s.substr(e), s === le && (e = g.indexOf(":"), e !== -1 ? (d = g.substring(0, e), d = u[d] || d, g = d + g.substring(e)) : g = E + ":" + g), R[s] = g;
          }
      if (D)
        for (o = 0, j = y.length; o < j; o++) {
          if (s = y[o++], g = y[o], e = s.indexOf(":"), e !== -1) {
            if (!(d = u[s.substring(0, e)])) {
              m(de(s.substring(0, e)));
              continue;
            }
            s = E === d ? s.substr(e + 1) : d + s.substr(e), s === le && (e = g.indexOf(":"), e !== -1 ? (d = g.substring(0, e), d = u[d] || d, g = d + g.substring(e)) : g = E + ":" + g);
          }
          R[s] = g;
        }
      return M = R;
    }
    function ve() {
      for (var S = /(\r\n|\r|\n)/g, I = 0, d = 0, E = 0, y = n, o, b; a >= E && (o = S.exec(t), !(!o || (y = o[0].length + o.index, y > a))); )
        I += 1, E = y;
      return a == -1 ? (d = y, b = t.substring(n)) : n === 0 ? b = t.substring(n, a) : (d = a - E, b = n == -1 ? t.substring(a) : t.substring(a, n + 1)), {
        data: b,
        line: I,
        column: d
      };
    }
    for (C = ve, O && (te = Object.create({}, {
      name: H(function() {
        return p;
      }),
      originalName: H(function() {
        return ee;
      }),
      attrs: H(ie),
      ns: H(function() {
        return u;
      })
    })); n !== -1; ) {
      if (t.charCodeAt(n) === 60 ? a = n : a = t.indexOf("<", n), a === -1) {
        if (L.length)
          return A("unexpected end of file");
        if (n === 0)
          return A("missing start tag");
        n < t.length && t.substring(n).trim() && m(ge);
        return;
      }
      if (n !== a) {
        if (L.length) {
          if (x && (x(t.substring(n, a), U, C), N))
            return;
        } else if (t.substring(n, a).trim() && (m(ge), N))
          return;
      }
      if (r = t.charCodeAt(a + 1), r === 33) {
        if (c = t.charCodeAt(a + 2), c === 91 && t.substr(a + 3, 6) === "CDATA[") {
          if (n = t.indexOf("]]>", a), n === -1)
            return A("unclosed cdata");
          if ($ && ($(t.substring(a + 9, n), C), N))
            return;
          n += 3;
          continue;
        }
        if (c === 45 && t.charCodeAt(a + 3) === 45) {
          if (n = t.indexOf("-->", a), n === -1)
            return A("unclosed comment");
          if (J && (J(t.substring(a + 4, n), U, C), N))
            return;
          n += 3;
          continue;
        }
      }
      if (r === 63) {
        if (n = t.indexOf("?>", a), n === -1)
          return A("unclosed question");
        if (K && (K(t.substring(a, n + 2), C), N))
          return;
        n += 2;
        continue;
      }
      for (l = a + 1; ; l++) {
        if (F = t.charCodeAt(l), isNaN(F))
          return n = -1, A("unclosed tag");
        if (F === 34)
          c = t.indexOf('"', l + 1), l = c !== -1 ? c : l;
        else if (F === 39)
          c = t.indexOf("'", l + 1), l = c !== -1 ? c : l;
        else if (F === 62) {
          n = l;
          break;
        }
      }
      if (r === 33) {
        if (V && (V(t.substring(a, n + 1), U, C), N))
          return;
        n += 1;
        continue;
      }
      if (M = {}, r === 47) {
        if (q = false, P = true, !L.length)
          return A("missing open tag");
        if (l = p = L.pop(), c = a + 2 + l.length, t.substring(a + 2, c) !== l)
          return A("closing tag mismatch");
        for (; c < n; c++)
          if (r = t.charCodeAt(c), !(r === 32 || r > 8 && r < 14))
            return A("close tag");
      } else {
        if (t.charCodeAt(n - 1) === 47 ? (l = p = t.substring(a + 1, n - 1), q = true, P = true) : (l = p = t.substring(a + 1, n), q = true, P = false), !(r > 96 && r < 123 || r > 64 && r < 91 || r === 95 || r === 58))
          return A("illegal first char nodeName");
        for (c = 1, fe = l.length; c < fe; c++)
          if (r = l.charCodeAt(c), !(r > 96 && r < 123 || r > 64 && r < 91 || r > 47 && r < 59 || r === 45 || r === 95 || r == 46)) {
            if (r === 32 || r < 14 && r > 8) {
              p = l.substring(0, c), M = null;
              break;
            }
            return A("invalid nodeName");
          }
        P || L.push(p);
      }
      if (T) {
        if (z = u, q && (P || h.push(z), M === null && (D = l.indexOf("xmlns", c) !== -1) && (re = c, ne = l, ie(), D = false)), ee = p, r = p.indexOf(":"), r !== -1) {
          if (W = u[p.substring(0, r)], !W)
            return A("missing namespace on <" + ee + ">");
          p = p.substr(r + 1);
        } else
          W = u.xmlns;
        W && (p = W + ":" + p);
      }
      if (q && (re = c, ne = l, v && (O ? v(te, U, P, C) : v(p, ie, U, P, C), N)))
        return;
      if (P) {
        if (Q && (Q(O ? te : p, U, q, C), N))
          return;
        T && (q ? u = z : u = h.pop());
      }
      n += 1;
    }
  }
}
exports.Parser = Ee;
exports.decode = U;
