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