import k from "../../../../quill-delta@5.1.0/node_modules/quill-delta/dist/Delta.mjs";
import { ClassAttributor as C, Scope as L } from "../../../../parchment@3.0.0/node_modules/parchment/dist/parchment.mjs";
import S from "../blots/inline.mjs";
import a from "../core/quill.mjs";
import v from "../core/module.mjs";
import { blockDelta as w } from "../blots/block.mjs";
import x from "../blots/break.mjs";
import T from "../blots/cursor.mjs";
import q, { escapeText as y } from "../blots/text.mjs";
import d, { CodeBlockContainer as E } from "../formats/code.mjs";
import { traverse as A } from "./clipboard.mjs";
const c = new C("code-token", "hljs", {
  scope: L.INLINE
});
class o extends S {
  static formats(e, t) {
    for (; e != null && e !== t.domNode; ) {
      if (e.classList && e.classList.contains(d.className))
        return super.formats(e, t);
      e = e.parentNode;
    }
  }
  constructor(e, t, i) {
    super(e, t, i), c.add(this.domNode, i);
  }
  format(e, t) {
    e !== o.blotName ? super.format(e, t) : t ? c.add(this.domNode, t) : (c.remove(this.domNode), this.domNode.classList.remove(this.statics.className));
  }
  optimize() {
    super.optimize(...arguments), c.value(this.domNode) || this.unwrap();
  }
}
o.blotName = "code-token";
o.className = "ql-token";
class n extends d {
  static create(e) {
    const t = super.create(e);
    return typeof e == "string" && t.setAttribute("data-language", e), t;
  }
  static formats(e) {
    return e.getAttribute("data-language") || "plain";
  }
  static register() {
  }
  // Syntax module will register
  format(e, t) {
    e === this.statics.blotName && t ? this.domNode.setAttribute("data-language", t) : super.format(e, t);
  }
  replaceWith(e, t) {
    return this.formatAt(0, this.length(), o.blotName, !1), super.replaceWith(e, t);
  }
}
class m extends E {
  attach() {
    super.attach(), this.forceNext = !1, this.scroll.emitMount(this);
  }
  format(e, t) {
    e === n.blotName && (this.forceNext = !0, this.children.forEach((i) => {
      i.format(e, t);
    }));
  }
  formatAt(e, t, i, s) {
    i === n.blotName && (this.forceNext = !0), super.formatAt(e, t, i, s);
  }
  highlight(e) {
    let t = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : !1;
    if (this.children.head == null)
      return;
    const s = `${Array.from(this.domNode.childNodes).filter((r) => r !== this.uiNode).map((r) => r.textContent).join(`
`)}
`, l = n.formats(this.children.head.domNode);
    if (t || this.forceNext || this.cachedText !== s) {
      if (s.trim().length > 0 || this.cachedText == null) {
        const r = this.children.reduce((u, g) => u.concat(w(g, !1)), new k()), f = e(s, l);
        r.diff(f).reduce((u, g) => {
          let {
            retain: p,
            attributes: N
          } = g;
          return p ? (N && Object.keys(N).forEach((b) => {
            [n.blotName, o.blotName].includes(b) && this.formatAt(u, p, b, N[b]);
          }), u + p) : u;
        }, 0);
      }
      this.cachedText = s, this.forceNext = !1;
    }
  }
  html(e, t) {
    const [i] = this.children.find(e);
    return `<pre data-language="${i ? n.formats(i.domNode) : "plain"}">
${y(this.code(e, t))}
</pre>`;
  }
  optimize(e) {
    if (super.optimize(e), this.parent != null && this.children.head != null && this.uiNode != null) {
      const t = n.formats(this.children.head.domNode);
      t !== this.uiNode.value && (this.uiNode.value = t);
    }
  }
}
m.allowedChildren = [n];
n.requiredContainer = m;
n.allowedChildren = [o, T, q, x];
const B = (h, e, t) => {
  if (typeof h.versionString == "string") {
    const i = h.versionString.split(".")[0];
    if (parseInt(i, 10) >= 11)
      return h.highlight(t, {
        language: e
      }).value;
  }
  return h.highlight(e, t).value;
};
class j extends v {
  static register() {
    a.register(o, !0), a.register(n, !0), a.register(m, !0);
  }
  constructor(e, t) {
    if (super(e, t), this.options.hljs == null)
      throw new Error("Syntax module requires highlight.js. Please include the library on the page before Quill.");
    this.languages = this.options.languages.reduce((i, s) => {
      let {
        key: l
      } = s;
      return i[l] = !0, i;
    }, {}), this.highlightBlot = this.highlightBlot.bind(this), this.initListener(), this.initTimer();
  }
  initListener() {
    this.quill.on(a.events.SCROLL_BLOT_MOUNT, (e) => {
      if (!(e instanceof m))
        return;
      const t = this.quill.root.ownerDocument.createElement("select");
      this.options.languages.forEach((i) => {
        let {
          key: s,
          label: l
        } = i;
        const r = t.ownerDocument.createElement("option");
        r.textContent = l, r.setAttribute("value", s), t.appendChild(r);
      }), t.addEventListener("change", () => {
        e.format(n.blotName, t.value), this.quill.root.focus(), this.highlight(e, !0);
      }), e.uiNode == null && (e.attachUI(t), e.children.head && (t.value = n.formats(e.children.head.domNode)));
    });
  }
  initTimer() {
    let e = null;
    this.quill.on(a.events.SCROLL_OPTIMIZE, () => {
      e && clearTimeout(e), e = setTimeout(() => {
        this.highlight(), e = null;
      }, this.options.interval);
    });
  }
  highlight() {
    let e = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : null, t = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : !1;
    if (this.quill.selection.composing)
      return;
    this.quill.update(a.sources.USER);
    const i = this.quill.getSelection();
    (e == null ? this.quill.scroll.descendants(m) : [e]).forEach((l) => {
      l.highlight(this.highlightBlot, t);
    }), this.quill.update(a.sources.SILENT), i != null && this.quill.setSelection(i, a.sources.SILENT);
  }
  highlightBlot(e) {
    let t = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "plain";
    if (t = this.languages[t] ? t : "plain", t === "plain")
      return y(e).split(`
`).reduce((s, l, r) => (r !== 0 && s.insert(`
`, {
        [d.blotName]: t
      }), s.insert(l)), new k());
    const i = this.quill.root.ownerDocument.createElement("div");
    return i.classList.add(d.className), i.innerHTML = B(this.options.hljs, t, e), A(this.quill.scroll, i, [(s, l) => {
      const r = c.value(s);
      return r ? l.compose(new k().retain(l.length(), {
        [o.blotName]: r
      })) : l;
    }], [(s, l) => s.data.split(`
`).reduce((r, f, u) => (u !== 0 && r.insert(`
`, {
      [d.blotName]: t
    }), r.insert(f)), l)], /* @__PURE__ */ new WeakMap());
  }
}
j.DEFAULTS = {
  hljs: window.hljs,
  interval: 1e3,
  languages: [{
    key: "plain",
    label: "Plain"
  }, {
    key: "bash",
    label: "Bash"
  }, {
    key: "cpp",
    label: "C++"
  }, {
    key: "cs",
    label: "C#"
  }, {
    key: "css",
    label: "CSS"
  }, {
    key: "diff",
    label: "Diff"
  }, {
    key: "xml",
    label: "HTML/XML"
  }, {
    key: "java",
    label: "Java"
  }, {
    key: "javascript",
    label: "JavaScript"
  }, {
    key: "markdown",
    label: "Markdown"
  }, {
    key: "php",
    label: "PHP"
  }, {
    key: "python",
    label: "Python"
  }, {
    key: "ruby",
    label: "Ruby"
  }, {
    key: "sql",
    label: "SQL"
  }]
};
export {
  n as CodeBlock,
  o as CodeToken,
  j as default
};
