var x = Object.defineProperty;
var T = (r, e, t) => e in r ? x(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t;
var d = (r, e, t) => (T(r, typeof e != "symbol" ? e + "" : e, t), t);
import { EmbedBlot as v, Scope as O } from "../../../../parchment@3.0.0/node_modules/parchment/dist/parchment.mjs";
import p from "./text.mjs";
const i = class i extends v {
  // Zero width no break space
  static value() {
  }
  constructor(e, t, s) {
    super(e, t), this.selection = s, this.textNode = document.createTextNode(i.CONTENTS), this.domNode.appendChild(this.textNode), this.savedLength = 0;
  }
  detach() {
    this.parent != null && this.parent.removeChild(this);
  }
  format(e, t) {
    if (this.savedLength !== 0) {
      super.format(e, t);
      return;
    }
    let s = this, n = 0;
    for (; s != null && s.statics.scope !== O.BLOCK_BLOT; )
      n += s.offset(s.parent), s = s.parent;
    s != null && (this.savedLength = i.CONTENTS.length, s.optimize(), s.formatAt(n, i.CONTENTS.length, e, t), this.savedLength = 0);
  }
  index(e, t) {
    return e === this.textNode ? 0 : super.index(e, t);
  }
  length() {
    return this.savedLength;
  }
  position() {
    return [this.textNode, this.textNode.data.length];
  }
  remove() {
    super.remove(), this.parent = null;
  }
  restore() {
    if (this.selection.composing || this.parent == null)
      return null;
    const e = this.selection.getNativeRange();
    for (; this.domNode.lastChild != null && this.domNode.lastChild !== this.textNode; )
      this.domNode.parentNode.insertBefore(this.domNode.lastChild, this.domNode);
    const t = this.prev instanceof p ? this.prev : null, s = t ? t.length() : 0, n = this.next instanceof p ? this.next : null, g = n ? n.text : "", {
      textNode: h
    } = this, a = h.data.split(i.CONTENTS).join("");
    h.data = i.CONTENTS;
    let o;
    if (t)
      o = t, (a || n) && (t.insertAt(t.length(), a + g), n && n.remove());
    else if (n)
      o = n, n.insertAt(0, a);
    else {
      const l = document.createTextNode(a);
      o = this.scroll.create(l), this.parent.insertBefore(o, this);
    }
    if (this.remove(), e) {
      const l = (N, c) => t && N === t.domNode ? c : N === h ? s + c - 1 : n && N === n.domNode ? s + a.length + c : null, f = l(e.start.node, e.start.offset), u = l(e.end.node, e.end.offset);
      if (f !== null && u !== null)
        return {
          startNode: o.domNode,
          startOffset: f,
          endNode: o.domNode,
          endOffset: u
        };
    }
    return null;
  }
  update(e, t) {
    if (e.some((s) => s.type === "characterData" && s.target === this.textNode)) {
      const s = this.restore();
      s && (t.range = s);
    }
  }
  // Avoid .ql-cursor being a descendant of `<a/>`.
  // The reason is Safari pushes down `<a/>` on text insertion.
  // That will cause DOM nodes not sync with the model.
  //
  // For example ({I} is the caret), given the markup:
  //    <a><span class="ql-cursor">\uFEFF{I}</span></a>
  // When typing a char "x", `<a/>` will be pushed down inside the `<span>` first:
  //    <span class="ql-cursor"><a>\uFEFF{I}</a></span>
  // And then "x" will be inserted after `<a/>`:
  //    <span class="ql-cursor"><a>\uFEFF</a>d{I}</span>
  optimize(e) {
    super.optimize(e);
    let {
      parent: t
    } = this;
    for (; t; ) {
      if (t.domNode.tagName === "A") {
        this.savedLength = i.CONTENTS.length, t.isolate(this.offset(t), this.length()).unwrap(), this.savedLength = 0;
        break;
      }
      t = t.parent;
    }
  }
  value() {
    return "";
  }
};
d(i, "blotName", "cursor"), d(i, "className", "ql-cursor"), d(i, "tagName", "span"), d(i, "CONTENTS", "\uFEFF");
let m = i;
export {
  m as default
};
