import { CODE_POINTS as e, getSurrogatePairCodePoint as t, isControlCodePoint as n, isSurrogate as r, isSurrogatePair as i, isUndefinedCodePoint as a } from "../common/unicode.js";
import { ERR as o } from "../common/error-codes.js";
//#region ../../node_modules/hast-util-raw/node_modules/parse5/dist/tokenizer/preprocessor.js
var s = 65536, c = class {
	constructor(e) {
		this.handler = e, this.html = "", this.pos = -1, this.lastGapPos = -2, this.gapStack = [], this.skipNextNewLine = !1, this.lastChunkWritten = !1, this.endOfChunkHit = !1, this.bufferWaterline = s, this.isEol = !1, this.lineStartPos = 0, this.droppedBufferSize = 0, this.line = 1, this.lastErrOffset = -1;
	}
	get col() {
		return this.pos - this.lineStartPos + Number(this.lastGapPos !== this.pos);
	}
	get offset() {
		return this.droppedBufferSize + this.pos;
	}
	getError(e, t) {
		let { line: n, col: r, offset: i } = this, a = r + t, o = i + t;
		return {
			code: e,
			startLine: n,
			endLine: n,
			startCol: a,
			endCol: a,
			startOffset: o,
			endOffset: o
		};
	}
	_err(e) {
		this.handler.onParseError && this.lastErrOffset !== this.offset && (this.lastErrOffset = this.offset, this.handler.onParseError(this.getError(e, 0)));
	}
	_addGap() {
		this.gapStack.push(this.lastGapPos), this.lastGapPos = this.pos;
	}
	_processSurrogate(n) {
		if (this.pos !== this.html.length - 1) {
			let e = this.html.charCodeAt(this.pos + 1);
			if (i(e)) return this.pos++, this._addGap(), t(n, e);
		} else if (!this.lastChunkWritten) return this.endOfChunkHit = !0, e.EOF;
		return this._err(o.surrogateInInputStream), n;
	}
	willDropParsedChunk() {
		return this.pos > this.bufferWaterline;
	}
	dropParsedChunk() {
		this.willDropParsedChunk() && (this.html = this.html.substring(this.pos), this.lineStartPos -= this.pos, this.droppedBufferSize += this.pos, this.pos = 0, this.lastGapPos = -2, this.gapStack.length = 0);
	}
	write(e, t) {
		this.html.length > 0 ? this.html += e : this.html = e, this.endOfChunkHit = !1, this.lastChunkWritten = t;
	}
	insertHtmlAtCurrentPos(e) {
		this.html = this.html.substring(0, this.pos + 1) + e + this.html.substring(this.pos + 1), this.endOfChunkHit = !1;
	}
	startsWith(e, t) {
		if (this.pos + e.length > this.html.length) return this.endOfChunkHit = !this.lastChunkWritten, !1;
		if (t) return this.html.startsWith(e, this.pos);
		for (let t = 0; t < e.length; t++) if ((this.html.charCodeAt(this.pos + t) | 32) !== e.charCodeAt(t)) return !1;
		return !0;
	}
	peek(t) {
		let n = this.pos + t;
		if (n >= this.html.length) return this.endOfChunkHit = !this.lastChunkWritten, e.EOF;
		let r = this.html.charCodeAt(n);
		return r === e.CARRIAGE_RETURN ? e.LINE_FEED : r;
	}
	advance() {
		if (this.pos++, this.isEol && (this.isEol = !1, this.line++, this.lineStartPos = this.pos), this.pos >= this.html.length) return this.endOfChunkHit = !this.lastChunkWritten, e.EOF;
		let t = this.html.charCodeAt(this.pos);
		return t === e.CARRIAGE_RETURN ? (this.isEol = !0, this.skipNextNewLine = !0, e.LINE_FEED) : t === e.LINE_FEED && (this.isEol = !0, this.skipNextNewLine) ? (this.line--, this.skipNextNewLine = !1, this._addGap(), this.advance()) : (this.skipNextNewLine = !1, r(t) && (t = this._processSurrogate(t)), this.handler.onParseError === null || t > 31 && t < 127 || t === e.LINE_FEED || t === e.CARRIAGE_RETURN || t > 159 && t < 64976 || this._checkForProblematicCharacters(t), t);
	}
	_checkForProblematicCharacters(e) {
		n(e) ? this._err(o.controlCharacterInInputStream) : a(e) && this._err(o.noncharacterInInputStream);
	}
	retreat(e) {
		for (this.pos -= e; this.pos < this.lastGapPos;) this.lastGapPos = this.gapStack.pop(), this.pos--;
		this.isEol = !1;
	}
};
//#endregion
export { c as Preprocessor };
