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