import "./generated/decode-data-html.js";
import { replaceCodePoint as e } from "./decode-codepoint.js";
//#region ../../node_modules/entities/dist/esm/decode.js
var t;
(function(e) {
	e[e.NUM = 35] = "NUM", e[e.SEMI = 59] = "SEMI", e[e.EQUALS = 61] = "EQUALS", e[e.ZERO = 48] = "ZERO", e[e.NINE = 57] = "NINE", e[e.LOWER_A = 97] = "LOWER_A", e[e.LOWER_F = 102] = "LOWER_F", e[e.LOWER_X = 120] = "LOWER_X", e[e.LOWER_Z = 122] = "LOWER_Z", e[e.UPPER_A = 65] = "UPPER_A", e[e.UPPER_F = 70] = "UPPER_F", e[e.UPPER_Z = 90] = "UPPER_Z";
})(t ||= {});
var n = 32, r;
(function(e) {
	e[e.VALUE_LENGTH = 49152] = "VALUE_LENGTH", e[e.BRANCH_LENGTH = 16256] = "BRANCH_LENGTH", e[e.JUMP_TABLE = 127] = "JUMP_TABLE";
})(r ||= {});
function i(e) {
	return e >= t.ZERO && e <= t.NINE;
}
function a(e) {
	return e >= t.UPPER_A && e <= t.UPPER_F || e >= t.LOWER_A && e <= t.LOWER_F;
}
function o(e) {
	return e >= t.UPPER_A && e <= t.UPPER_Z || e >= t.LOWER_A && e <= t.LOWER_Z || i(e);
}
function s(e) {
	return e === t.EQUALS || o(e);
}
var c;
(function(e) {
	e[e.EntityStart = 0] = "EntityStart", e[e.NumericStart = 1] = "NumericStart", e[e.NumericDecimal = 2] = "NumericDecimal", e[e.NumericHex = 3] = "NumericHex", e[e.NamedEntity = 4] = "NamedEntity";
})(c ||= {});
var l;
(function(e) {
	e[e.Legacy = 0] = "Legacy", e[e.Strict = 1] = "Strict", e[e.Attribute = 2] = "Attribute";
})(l ||= {});
var u = class {
	constructor(e, t, n) {
		this.decodeTree = e, this.emitCodePoint = t, this.errors = n, this.state = c.EntityStart, this.consumed = 1, this.result = 0, this.treeIndex = 0, this.excess = 1, this.decodeMode = l.Strict;
	}
	startEntity(e) {
		this.decodeMode = e, this.state = c.EntityStart, this.result = 0, this.treeIndex = 0, this.excess = 1, this.consumed = 1;
	}
	write(e, n) {
		switch (this.state) {
			case c.EntityStart: return e.charCodeAt(n) === t.NUM ? (this.state = c.NumericStart, this.consumed += 1, this.stateNumericStart(e, n + 1)) : (this.state = c.NamedEntity, this.stateNamedEntity(e, n));
			case c.NumericStart: return this.stateNumericStart(e, n);
			case c.NumericDecimal: return this.stateNumericDecimal(e, n);
			case c.NumericHex: return this.stateNumericHex(e, n);
			case c.NamedEntity: return this.stateNamedEntity(e, n);
		}
	}
	stateNumericStart(e, r) {
		return r >= e.length ? -1 : (e.charCodeAt(r) | n) === t.LOWER_X ? (this.state = c.NumericHex, this.consumed += 1, this.stateNumericHex(e, r + 1)) : (this.state = c.NumericDecimal, this.stateNumericDecimal(e, r));
	}
	addToNumericResult(e, t, n, r) {
		if (t !== n) {
			let i = n - t;
			this.result = this.result * r ** +i + Number.parseInt(e.substr(t, i), r), this.consumed += i;
		}
	}
	stateNumericHex(e, t) {
		let n = t;
		for (; t < e.length;) {
			let r = e.charCodeAt(t);
			if (i(r) || a(r)) t += 1;
			else return this.addToNumericResult(e, n, t, 16), this.emitNumericEntity(r, 3);
		}
		return this.addToNumericResult(e, n, t, 16), -1;
	}
	stateNumericDecimal(e, t) {
		let n = t;
		for (; t < e.length;) {
			let r = e.charCodeAt(t);
			if (i(r)) t += 1;
			else return this.addToNumericResult(e, n, t, 10), this.emitNumericEntity(r, 2);
		}
		return this.addToNumericResult(e, n, t, 10), -1;
	}
	emitNumericEntity(n, r) {
		var i;
		if (this.consumed <= r) return (i = this.errors) == null || i.absenceOfDigitsInNumericCharacterReference(this.consumed), 0;
		if (n === t.SEMI) this.consumed += 1;
		else if (this.decodeMode === l.Strict) return 0;
		return this.emitCodePoint(e(this.result), this.consumed), this.errors && (n !== t.SEMI && this.errors.missingSemicolonAfterCharacterReference(), this.errors.validateNumericCharacterReference(this.result)), this.consumed;
	}
	stateNamedEntity(e, n) {
		let { decodeTree: i } = this, a = i[this.treeIndex], o = (a & r.VALUE_LENGTH) >> 14;
		for (; n < e.length; n++, this.excess++) {
			let c = e.charCodeAt(n);
			if (this.treeIndex = d(i, a, this.treeIndex + Math.max(1, o), c), this.treeIndex < 0) return this.result === 0 || this.decodeMode === l.Attribute && (o === 0 || s(c)) ? 0 : this.emitNotTerminatedNamedEntity();
			if (a = i[this.treeIndex], o = (a & r.VALUE_LENGTH) >> 14, o !== 0) {
				if (c === t.SEMI) return this.emitNamedEntityData(this.treeIndex, o, this.consumed + this.excess);
				this.decodeMode !== l.Strict && (this.result = this.treeIndex, this.consumed += this.excess, this.excess = 0);
			}
		}
		return -1;
	}
	emitNotTerminatedNamedEntity() {
		var e;
		let { result: t, decodeTree: n } = this, i = (n[t] & r.VALUE_LENGTH) >> 14;
		return this.emitNamedEntityData(t, i, this.consumed), (e = this.errors) == null || e.missingSemicolonAfterCharacterReference(), this.consumed;
	}
	emitNamedEntityData(e, t, n) {
		let { decodeTree: i } = this;
		return this.emitCodePoint(t === 1 ? i[e] & ~r.VALUE_LENGTH : i[e + 1], n), t === 3 && this.emitCodePoint(i[e + 2], n), n;
	}
	end() {
		var e;
		switch (this.state) {
			case c.NamedEntity: return this.result !== 0 && (this.decodeMode !== l.Attribute || this.result === this.treeIndex) ? this.emitNotTerminatedNamedEntity() : 0;
			case c.NumericDecimal: return this.emitNumericEntity(0, 2);
			case c.NumericHex: return this.emitNumericEntity(0, 3);
			case c.NumericStart: return (e = this.errors) == null || e.absenceOfDigitsInNumericCharacterReference(this.consumed), 0;
			case c.EntityStart: return 0;
		}
	}
};
function d(e, t, n, i) {
	let a = (t & r.BRANCH_LENGTH) >> 7, o = t & r.JUMP_TABLE;
	if (a === 0) return o !== 0 && i === o ? n : -1;
	if (o) {
		let t = i - o;
		return t < 0 || t >= a ? -1 : e[n + t] - 1;
	}
	let s = n, c = s + a - 1;
	for (; s <= c;) {
		let t = s + c >>> 1, n = e[t];
		if (n < i) s = t + 1;
		else if (n > i) c = t - 1;
		else return e[t + a];
	}
	return -1;
}
//#endregion
export { l as DecodingMode, u as EntityDecoder };
