import { ElementType as i, isTag as p } from "./index-esm6.js";
class o {
  constructor() {
    this.parent = null, this.prev = null, this.next = null, this.startIndex = null, this.endIndex = null;
  }
  // Read-write aliases for properties
  /**
   * Same as {@link parent}.
   * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
   */
  get parentNode() {
    return this.parent;
  }
  set parentNode(e) {
    this.parent = e;
  }
  /**
   * Same as {@link prev}.
   * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
   */
  get previousSibling() {
    return this.prev;
  }
  set previousSibling(e) {
    this.prev = e;
  }
  /**
   * Same as {@link next}.
   * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
   */
  get nextSibling() {
    return this.next;
  }
  set nextSibling(e) {
    this.next = e;
  }
  /**
   * Clone this node, and optionally its children.
   *
   * @param recursive Clone child nodes as well.
   * @returns A clone of the node.
   */
  cloneNode(e = !1) {
    return h(this, e);
  }
}
class l extends o {
  /**
   * @param data The content of the data node
   */
  constructor(e) {
    super(), this.data = e;
  }
  /**
   * Same as {@link data}.
   * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
   */
  get nodeValue() {
    return this.data;
  }
  set nodeValue(e) {
    this.data = e;
  }
}
class x extends l {
  constructor() {
    super(...arguments), this.type = i.Text;
  }
  get nodeType() {
    return 3;
  }
}
class d extends l {
  constructor() {
    super(...arguments), this.type = i.Comment;
  }
  get nodeType() {
    return 8;
  }
}
class m extends l {
  constructor(e, s) {
    super(s), this.name = e, this.type = i.Directive;
  }
  get nodeType() {
    return 1;
  }
}
class u extends o {
  /**
   * @param children Children of the node. Only certain node types can have children.
   */
  constructor(e) {
    super(), this.children = e;
  }
  // Aliases
  /** First child of the node. */
  get firstChild() {
    var e;
    return (e = this.children[0]) !== null && e !== void 0 ? e : null;
  }
  /** Last child of the node. */
  get lastChild() {
    return this.children.length > 0 ? this.children[this.children.length - 1] : null;
  }
  /**
   * Same as {@link children}.
   * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
   */
  get childNodes() {
    return this.children;
  }
  set childNodes(e) {
    this.children = e;
  }
}
class f extends u {
  constructor() {
    super(...arguments), this.type = i.CDATA;
  }
  get nodeType() {
    return 4;
  }
}
class g extends u {
  constructor() {
    super(...arguments), this.type = i.Root;
  }
  get nodeType() {
    return 9;
  }
}
class y extends u {
  /**
   * @param name Name of the tag, eg. `div`, `span`.
   * @param attribs Object mapping attribute names to attribute values.
   * @param children Children of the node.
   */
  constructor(e, s, r = [], n = e === "script" ? i.Script : e === "style" ? i.Style : i.Tag) {
    super(r), this.name = e, this.attribs = s, this.type = n;
  }
  get nodeType() {
    return 1;
  }
  // DOM Level 1 aliases
  /**
   * Same as {@link name}.
   * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
   */
  get tagName() {
    return this.name;
  }
  set tagName(e) {
    this.name = e;
  }
  get attributes() {
    return Object.keys(this.attribs).map((e) => {
      var s, r;
      return {
        name: e,
        value: this.attribs[e],
        namespace: (s = this["x-attribsNamespace"]) === null || s === void 0 ? void 0 : s[e],
        prefix: (r = this["x-attribsPrefix"]) === null || r === void 0 ? void 0 : r[e]
      };
    });
  }
}
function b(t) {
  return p(t);
}
function T(t) {
  return t.type === i.CDATA;
}
function N(t) {
  return t.type === i.Text;
}
function v(t) {
  return t.type === i.Comment;
}
function C(t) {
  return t.type === i.Directive;
}
function I(t) {
  return t.type === i.Root;
}
function h(t, e = !1) {
  let s;
  if (N(t))
    s = new x(t.data);
  else if (v(t))
    s = new d(t.data);
  else if (b(t)) {
    const r = e ? c(t.children) : [], n = new y(t.name, { ...t.attribs }, r);
    r.forEach((a) => a.parent = n), t.namespace != null && (n.namespace = t.namespace), t["x-attribsNamespace"] && (n["x-attribsNamespace"] = { ...t["x-attribsNamespace"] }), t["x-attribsPrefix"] && (n["x-attribsPrefix"] = { ...t["x-attribsPrefix"] }), s = n;
  } else if (T(t)) {
    const r = e ? c(t.children) : [], n = new f(r);
    r.forEach((a) => a.parent = n), s = n;
  } else if (I(t)) {
    const r = e ? c(t.children) : [], n = new g(r);
    r.forEach((a) => a.parent = n), t["x-mode"] && (n["x-mode"] = t["x-mode"]), s = n;
  } else if (C(t)) {
    const r = new m(t.name, t.data);
    t["x-name"] != null && (r["x-name"] = t["x-name"], r["x-publicId"] = t["x-publicId"], r["x-systemId"] = t["x-systemId"]), s = r;
  } else
    throw new Error(`Not implemented yet: ${t.type}`);
  return s.startIndex = t.startIndex, s.endIndex = t.endIndex, t.sourceCodeLocation != null && (s.sourceCodeLocation = t.sourceCodeLocation), s;
}
function c(t) {
  const e = t.map((s) => h(s, !0));
  for (let s = 1; s < e.length; s++)
    e[s].prev = e[s - 1], e[s - 1].next = e[s];
  return e;
}
export {
  f as CDATA,
  d as Comment,
  l as DataNode,
  g as Document,
  y as Element,
  o as Node,
  u as NodeWithChildren,
  m as ProcessingInstruction,
  x as Text,
  h as cloneNode,
  T as isCDATA,
  v as isComment,
  C as isDirective,
  I as isDocument,
  b as isTag,
  N as isText
};
