import { asciiAlphanumeric, asciiDigit, asciiHexDigit } from "../../micromark-util-character/index.js";
import { decodeNamedCharacterReference } from "../../decode-named-character-reference/index.dom.js";
const characterReference = {
	name: "characterReference",
	tokenize: tokenizeCharacterReference
};
function tokenizeCharacterReference(i, a, o) {
	let s = this, c = 0, l, u;
	return d;
	function d(e) {
		return i.enter("characterReference"), i.enter("characterReferenceMarker"), i.consume(e), i.exit("characterReferenceMarker"), f;
	}
	function f(n) {
		return n === 35 ? (i.enter("characterReferenceMarkerNumeric"), i.consume(n), i.exit("characterReferenceMarkerNumeric"), p) : (i.enter("characterReferenceValue"), l = 31, u = asciiAlphanumeric, m(n));
	}
	function p(e) {
		return e === 88 || e === 120 ? (i.enter("characterReferenceMarkerHexadecimal"), i.consume(e), i.exit("characterReferenceMarkerHexadecimal"), i.enter("characterReferenceValue"), l = 6, u = asciiHexDigit, m) : (i.enter("characterReferenceValue"), l = 7, u = asciiDigit, m(e));
	}
	function m(n) {
		if (n === 59 && c) {
			let r = i.exit("characterReferenceValue");
			return u === asciiAlphanumeric && !decodeNamedCharacterReference(s.sliceSerialize(r)) ? o(n) : (i.enter("characterReferenceMarker"), i.consume(n), i.exit("characterReferenceMarker"), i.exit("characterReference"), a);
		}
		return u(n) && c++ < l ? (i.consume(n), m) : o(n);
	}
}
export { characterReference };
