import { NS as e, NUMBERED_HEADERS as t, TAG_ID as n } from "../common/html.js";
//#region ../../node_modules/hast-util-raw/node_modules/parse5/dist/parser/open-element-stack.js
var r = new Set([
	n.DD,
	n.DT,
	n.LI,
	n.OPTGROUP,
	n.OPTION,
	n.P,
	n.RB,
	n.RP,
	n.RT,
	n.RTC
]), i = new Set([
	...r,
	n.CAPTION,
	n.COLGROUP,
	n.TBODY,
	n.TD,
	n.TFOOT,
	n.TH,
	n.THEAD,
	n.TR
]), a = new Set([
	n.APPLET,
	n.CAPTION,
	n.HTML,
	n.MARQUEE,
	n.OBJECT,
	n.TABLE,
	n.TD,
	n.TEMPLATE,
	n.TH
]), o = new Set([
	...a,
	n.OL,
	n.UL
]), s = new Set([...a, n.BUTTON]), c = new Set([
	n.ANNOTATION_XML,
	n.MI,
	n.MN,
	n.MO,
	n.MS,
	n.MTEXT
]), l = new Set([
	n.DESC,
	n.FOREIGN_OBJECT,
	n.TITLE
]), u = new Set([
	n.TR,
	n.TEMPLATE,
	n.HTML
]), d = new Set([
	n.TBODY,
	n.TFOOT,
	n.THEAD,
	n.TEMPLATE,
	n.HTML
]), f = new Set([
	n.TABLE,
	n.TEMPLATE,
	n.HTML
]), p = new Set([n.TD, n.TH]), m = class {
	get currentTmplContentOrNode() {
		return this._isInTemplate() ? this.treeAdapter.getTemplateContent(this.current) : this.current;
	}
	constructor(e, t, r) {
		this.treeAdapter = t, this.handler = r, this.items = [], this.tagIDs = [], this.stackTop = -1, this.tmplCount = 0, this.currentTagId = n.UNKNOWN, this.current = e;
	}
	_indexOf(e) {
		return this.items.lastIndexOf(e, this.stackTop);
	}
	_isInTemplate() {
		return this.currentTagId === n.TEMPLATE && this.treeAdapter.getNamespaceURI(this.current) === e.HTML;
	}
	_updateCurrentElement() {
		this.current = this.items[this.stackTop], this.currentTagId = this.tagIDs[this.stackTop];
	}
	push(e, t) {
		this.stackTop++, this.items[this.stackTop] = e, this.current = e, this.tagIDs[this.stackTop] = t, this.currentTagId = t, this._isInTemplate() && this.tmplCount++, this.handler.onItemPush(e, t, !0);
	}
	pop() {
		let e = this.current;
		this.tmplCount > 0 && this._isInTemplate() && this.tmplCount--, this.stackTop--, this._updateCurrentElement(), this.handler.onItemPop(e, !0);
	}
	replace(e, t) {
		let n = this._indexOf(e);
		this.items[n] = t, n === this.stackTop && (this.current = t);
	}
	insertAfter(e, t, n) {
		let r = this._indexOf(e) + 1;
		this.items.splice(r, 0, t), this.tagIDs.splice(r, 0, n), this.stackTop++, r === this.stackTop && this._updateCurrentElement(), this.current && this.currentTagId !== void 0 && this.handler.onItemPush(this.current, this.currentTagId, r === this.stackTop);
	}
	popUntilTagNamePopped(t) {
		let n = this.stackTop + 1;
		do
			n = this.tagIDs.lastIndexOf(t, n - 1);
		while (n > 0 && this.treeAdapter.getNamespaceURI(this.items[n]) !== e.HTML);
		this.shortenToLength(Math.max(n, 0));
	}
	shortenToLength(e) {
		for (; this.stackTop >= e;) {
			let t = this.current;
			this.tmplCount > 0 && this._isInTemplate() && --this.tmplCount, this.stackTop--, this._updateCurrentElement(), this.handler.onItemPop(t, this.stackTop < e);
		}
	}
	popUntilElementPopped(e) {
		let t = this._indexOf(e);
		this.shortenToLength(Math.max(t, 0));
	}
	popUntilPopped(e, t) {
		let n = this._indexOfTagNames(e, t);
		this.shortenToLength(Math.max(n, 0));
	}
	popUntilNumberedHeaderPopped() {
		this.popUntilPopped(t, e.HTML);
	}
	popUntilTableCellPopped() {
		this.popUntilPopped(p, e.HTML);
	}
	popAllUpToHtmlElement() {
		this.tmplCount = 0, this.shortenToLength(1);
	}
	_indexOfTagNames(e, t) {
		for (let n = this.stackTop; n >= 0; n--) if (e.has(this.tagIDs[n]) && this.treeAdapter.getNamespaceURI(this.items[n]) === t) return n;
		return -1;
	}
	clearBackTo(e, t) {
		let n = this._indexOfTagNames(e, t);
		this.shortenToLength(n + 1);
	}
	clearBackToTableContext() {
		this.clearBackTo(f, e.HTML);
	}
	clearBackToTableBodyContext() {
		this.clearBackTo(d, e.HTML);
	}
	clearBackToTableRowContext() {
		this.clearBackTo(u, e.HTML);
	}
	remove(e) {
		let t = this._indexOf(e);
		t >= 0 && (t === this.stackTop ? this.pop() : (this.items.splice(t, 1), this.tagIDs.splice(t, 1), this.stackTop--, this._updateCurrentElement(), this.handler.onItemPop(e, !1)));
	}
	tryPeekProperlyNestedBodyElement() {
		return this.stackTop >= 1 && this.tagIDs[1] === n.BODY ? this.items[1] : null;
	}
	contains(e) {
		return this._indexOf(e) > -1;
	}
	getCommonAncestor(e) {
		let t = this._indexOf(e) - 1;
		return t >= 0 ? this.items[t] : null;
	}
	isRootHtmlElementCurrent() {
		return this.stackTop === 0 && this.tagIDs[0] === n.HTML;
	}
	hasInDynamicScope(t, n) {
		for (let r = this.stackTop; r >= 0; r--) {
			let i = this.tagIDs[r];
			switch (this.treeAdapter.getNamespaceURI(this.items[r])) {
				case e.HTML:
					if (i === t) return !0;
					if (n.has(i)) return !1;
					break;
				case e.SVG:
					if (l.has(i)) return !1;
					break;
				case e.MATHML:
					if (c.has(i)) return !1;
					break;
			}
		}
		return !0;
	}
	hasInScope(e) {
		return this.hasInDynamicScope(e, a);
	}
	hasInListItemScope(e) {
		return this.hasInDynamicScope(e, o);
	}
	hasInButtonScope(e) {
		return this.hasInDynamicScope(e, s);
	}
	hasNumberedHeaderInScope() {
		for (let n = this.stackTop; n >= 0; n--) {
			let r = this.tagIDs[n];
			switch (this.treeAdapter.getNamespaceURI(this.items[n])) {
				case e.HTML:
					if (t.has(r)) return !0;
					if (a.has(r)) return !1;
					break;
				case e.SVG:
					if (l.has(r)) return !1;
					break;
				case e.MATHML:
					if (c.has(r)) return !1;
					break;
			}
		}
		return !0;
	}
	hasInTableScope(t) {
		for (let r = this.stackTop; r >= 0; r--) if (this.treeAdapter.getNamespaceURI(this.items[r]) === e.HTML) switch (this.tagIDs[r]) {
			case t: return !0;
			case n.TABLE:
			case n.HTML: return !1;
		}
		return !0;
	}
	hasTableBodyContextInTableScope() {
		for (let t = this.stackTop; t >= 0; t--) if (this.treeAdapter.getNamespaceURI(this.items[t]) === e.HTML) switch (this.tagIDs[t]) {
			case n.TBODY:
			case n.THEAD:
			case n.TFOOT: return !0;
			case n.TABLE:
			case n.HTML: return !1;
		}
		return !0;
	}
	hasInSelectScope(t) {
		for (let r = this.stackTop; r >= 0; r--) if (this.treeAdapter.getNamespaceURI(this.items[r]) === e.HTML) switch (this.tagIDs[r]) {
			case t: return !0;
			case n.OPTION:
			case n.OPTGROUP: break;
			default: return !1;
		}
		return !0;
	}
	generateImpliedEndTags() {
		for (; this.currentTagId !== void 0 && r.has(this.currentTagId);) this.pop();
	}
	generateImpliedEndTagsThoroughly() {
		for (; this.currentTagId !== void 0 && i.has(this.currentTagId);) this.pop();
	}
	generateImpliedEndTagsWithExclusion(e) {
		for (; this.currentTagId !== void 0 && this.currentTagId !== e && i.has(this.currentTagId);) this.pop();
	}
};
//#endregion
export { m as OpenElementStack };
