var F = Object.defineProperty;
var q = (i, t, e) => t in i ? F(i, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : i[t] = e;
var p = (i, t, e) => (q(i, typeof t != "symbol" ? t + "" : t, e), e);
import * as V from "../../../../parchment@3.0.0/node_modules/parchment/dist/parchment.mjs";
import { ParentBlot as $, ScrollBlot as H, Scope as j, Registry as k } from "../../../../parchment@3.0.0/node_modules/parchment/dist/parchment.mjs";
import c from "../../../../quill-delta@5.1.0/node_modules/quill-delta/dist/Delta.mjs";
import G from "./editor.mjs";
import l from "./emitter.mjs";
import A from "./instances.mjs";
import D from "./logger.mjs";
import X from "./module.mjs";
import z, { Range as B } from "./selection.mjs";
import K from "./composition.mjs";
import U from "./theme.mjs";
import W from "./utils/scrollRectIntoView.mjs";
import J from "./utils/createRegistryWithFormats.mjs";
import M from "../../../../lodash-es@4.17.21/node_modules/lodash-es/merge.mjs";
const y = D("quill"), E = new k();
$.uiClass = "ql-ui";
const m = class m {
  static debug(t) {
    t === !0 && (t = "log"), D.level(t);
  }
  static find(t) {
    let e = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : !1;
    return A.get(t) || E.find(t, e);
  }
  static import(t) {
    return this.imports[t] == null && y.error(`Cannot import ${t}. Are you sure it was registered?`), this.imports[t];
  }
  static register() {
    if (typeof (arguments.length <= 0 ? void 0 : arguments[0]) != "string") {
      const t = arguments.length <= 0 ? void 0 : arguments[0], e = !!(!(arguments.length <= 1) && arguments[1]), n = "attrName" in t ? t.attrName : t.blotName;
      typeof n == "string" ? this.register(`formats/${n}`, t, e) : Object.keys(t).forEach((s) => {
        this.register(s, t[s], e);
      });
    } else {
      const t = arguments.length <= 0 ? void 0 : arguments[0], e = arguments.length <= 1 ? void 0 : arguments[1], n = !!(!(arguments.length <= 2) && arguments[2]);
      this.imports[t] != null && !n && y.warn(`Overwriting ${t} with`, e), this.imports[t] = e, (t.startsWith("blots/") || t.startsWith("formats/")) && e && typeof e != "boolean" && e.blotName !== "abstract" && E.register(e), typeof e.register == "function" && e.register(E);
    }
  }
  constructor(t) {
    let e = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
    if (this.options = Y(t, e), this.container = this.options.container, this.container == null) {
      y.error("Invalid Quill container", t);
      return;
    }
    this.options.debug && m.debug(this.options.debug);
    const n = this.container.innerHTML.trim();
    this.container.classList.add("ql-container"), this.container.innerHTML = "", A.set(this.container, this), this.root = this.addContainer("ql-editor"), this.root.classList.add("ql-blank"), this.emitter = new l();
    const s = H.blotName, r = this.options.registry.query(s);
    if (!r || !("blotName" in r))
      throw new Error(`Cannot initialize Quill without "${s}" blot`);
    if (this.scroll = new r(this.options.registry, this.root, {
      emitter: this.emitter
    }), this.editor = new G(this.scroll), this.selection = new z(this.scroll, this.emitter), this.composition = new K(this.scroll, this.emitter), this.theme = new this.options.theme(this, this.options), this.keyboard = this.theme.addModule("keyboard"), this.clipboard = this.theme.addModule("clipboard"), this.history = this.theme.addModule("history"), this.uploader = this.theme.addModule("uploader"), this.theme.addModule("input"), this.theme.addModule("uiNode"), this.theme.init(), this.emitter.on(l.events.EDITOR_CHANGE, (o) => {
      o === l.events.TEXT_CHANGE && this.root.classList.toggle("ql-blank", this.editor.isBlank());
    }), this.emitter.on(l.events.SCROLL_UPDATE, (o, u) => {
      const d = this.selection.lastRange, [a] = this.selection.getRange(), w = d && a ? {
        oldRange: d,
        newRange: a
      } : void 0;
      h.call(this, () => this.editor.update(null, u, w), o);
    }), this.emitter.on(l.events.SCROLL_EMBED_UPDATE, (o, u) => {
      const d = this.selection.lastRange, [a] = this.selection.getRange(), w = d && a ? {
        oldRange: d,
        newRange: a
      } : void 0;
      h.call(this, () => {
        const f = new c().retain(o.offset(this)).retain({
          [o.statics.blotName]: u
        });
        return this.editor.update(f, [], w);
      }, m.sources.USER);
    }), n) {
      const o = this.clipboard.convert({
        html: `${n}<p><br></p>`,
        text: `
`
      });
      this.setContents(o);
    }
    this.history.clear(), this.options.placeholder && this.root.setAttribute("data-placeholder", this.options.placeholder), this.options.readOnly && this.disable(), this.allowReadOnlyEdits = !1;
  }
  addContainer(t) {
    let e = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : null;
    if (typeof t == "string") {
      const n = t;
      t = document.createElement("div"), t.classList.add(n);
    }
    return this.container.insertBefore(t, e), t;
  }
  blur() {
    this.selection.setRange(null);
  }
  deleteText(t, e, n) {
    return [t, e, , n] = g(t, e, n), h.call(this, () => this.editor.deleteText(t, e), n, t, -1 * e);
  }
  disable() {
    this.enable(!1);
  }
  editReadOnly(t) {
    this.allowReadOnlyEdits = !0;
    const e = t();
    return this.allowReadOnlyEdits = !1, e;
  }
  enable() {
    let t = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : !0;
    this.scroll.enable(t), this.container.classList.toggle("ql-disabled", !t);
  }
  focus() {
    let t = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
    this.selection.focus(), t.preventScroll || this.scrollSelectionIntoView();
  }
  format(t, e) {
    let n = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : l.sources.API;
    return h.call(this, () => {
      const s = this.getSelection(!0);
      let r = new c();
      if (s == null)
        return r;
      if (this.scroll.query(t, j.BLOCK))
        r = this.editor.formatLine(s.index, s.length, {
          [t]: e
        });
      else {
        if (s.length === 0)
          return this.selection.format(t, e), r;
        r = this.editor.formatText(s.index, s.length, {
          [t]: e
        });
      }
      return this.setSelection(s, l.sources.SILENT), r;
    }, n);
  }
  formatLine(t, e, n, s, r) {
    let o;
    return [t, e, o, r] = g(
      t,
      e,
      // @ts-expect-error
      n,
      s,
      r
    ), h.call(this, () => this.editor.formatLine(t, e, o), r, t, 0);
  }
  formatText(t, e, n, s, r) {
    let o;
    return [t, e, o, r] = g(
      // @ts-expect-error
      t,
      e,
      n,
      s,
      r
    ), h.call(this, () => this.editor.formatText(t, e, o), r, t, 0);
  }
  getBounds(t) {
    let e = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 0, n = null;
    if (typeof t == "number" ? n = this.selection.getBounds(t, e) : n = this.selection.getBounds(t.index, t.length), !n)
      return null;
    const s = this.container.getBoundingClientRect();
    return {
      bottom: n.bottom - s.top,
      height: n.height,
      left: n.left - s.left,
      right: n.right - s.left,
      top: n.top - s.top,
      width: n.width
    };
  }
  getContents() {
    let t = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : 0, e = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : this.getLength() - t;
    return [t, e] = g(t, e), this.editor.getContents(t, e);
  }
  getFormat() {
    let t = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : this.getSelection(!0), e = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 0;
    return typeof t == "number" ? this.editor.getFormat(t, e) : this.editor.getFormat(t.index, t.length);
  }
  getIndex(t) {
    return t.offset(this.scroll);
  }
  getLength() {
    return this.scroll.length();
  }
  getLeaf(t) {
    return this.scroll.leaf(t);
  }
  getLine(t) {
    return this.scroll.line(t);
  }
  getLines() {
    let t = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : 0, e = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : Number.MAX_VALUE;
    return typeof t != "number" ? this.scroll.lines(t.index, t.length) : this.scroll.lines(t, e);
  }
  getModule(t) {
    return this.theme.modules[t];
  }
  getSelection() {
    return (arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : !1) && this.focus(), this.update(), this.selection.getRange()[0];
  }
  getSemanticHTML() {
    let t = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : 0, e = arguments.length > 1 ? arguments[1] : void 0;
    return typeof t == "number" && (e = e ?? this.getLength() - t), [t, e] = g(t, e), this.editor.getHTML(t, e);
  }
  getText() {
    let t = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : 0, e = arguments.length > 1 ? arguments[1] : void 0;
    return typeof t == "number" && (e = e ?? this.getLength() - t), [t, e] = g(t, e), this.editor.getText(t, e);
  }
  hasFocus() {
    return this.selection.hasFocus();
  }
  insertEmbed(t, e, n) {
    let s = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : m.sources.API;
    return h.call(this, () => this.editor.insertEmbed(t, e, n), s, t);
  }
  insertText(t, e, n, s, r) {
    let o;
    return [t, , o, r] = g(t, 0, n, s, r), h.call(this, () => this.editor.insertText(t, e, o), r, t, e.length);
  }
  isEnabled() {
    return this.scroll.isEnabled();
  }
  off() {
    return this.emitter.off(...arguments);
  }
  on() {
    return this.emitter.on(...arguments);
  }
  once() {
    return this.emitter.once(...arguments);
  }
  removeFormat(t, e, n) {
    return [t, e, , n] = g(t, e, n), h.call(this, () => this.editor.removeFormat(t, e), n, t);
  }
  scrollRectIntoView(t) {
    W(this.root, t);
  }
  /**
   * @deprecated Use Quill#scrollSelectionIntoView() instead.
   */
  scrollIntoView() {
    console.warn("Quill#scrollIntoView() has been deprecated and will be removed in the near future. Please use Quill#scrollSelectionIntoView() instead."), this.scrollSelectionIntoView();
  }
  /**
   * Scroll the current selection into the visible area.
   * If the selection is already visible, no scrolling will occur.
   */
  scrollSelectionIntoView() {
    const t = this.selection.lastRange, e = t && this.selection.getBounds(t.index, t.length);
    e && this.scrollRectIntoView(e);
  }
  setContents(t) {
    let e = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : l.sources.API;
    return h.call(this, () => {
      t = new c(t);
      const n = this.getLength(), s = this.editor.deleteText(0, n), r = this.editor.insertContents(0, t), o = this.editor.deleteText(this.getLength() - 1, 1);
      return s.compose(r).compose(o);
    }, e);
  }
  setSelection(t, e, n) {
    t == null ? this.selection.setRange(null, e || m.sources.API) : ([t, e, , n] = g(t, e, n), this.selection.setRange(new B(Math.max(0, t), e), n), n !== l.sources.SILENT && this.scrollSelectionIntoView());
  }
  setText(t) {
    let e = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : l.sources.API;
    const n = new c().insert(t);
    return this.setContents(n, e);
  }
  update() {
    let t = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : l.sources.USER;
    const e = this.scroll.update(t);
    return this.selection.update(t), e;
  }
  updateContents(t) {
    let e = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : l.sources.API;
    return h.call(this, () => (t = new c(t), this.editor.applyDelta(t)), e, !0);
  }
};
p(m, "DEFAULTS", {
  bounds: null,
  modules: {
    clipboard: !0,
    keyboard: !0,
    history: !0,
    uploader: !0
  },
  placeholder: "",
  readOnly: !1,
  registry: E,
  theme: "default"
}), p(m, "events", l.events), p(m, "sources", l.sources), p(m, "version", "2.0.2"), p(m, "imports", {
  delta: c,
  parchment: V,
  "core/module": X,
  "core/theme": U
});
let b = m;
function N(i) {
  return typeof i == "string" ? document.querySelector(i) : i;
}
function T(i) {
  return Object.entries(i ?? {}).reduce((t, e) => {
    let [n, s] = e;
    return {
      ...t,
      [n]: s === !0 ? {} : s
    };
  }, {});
}
function O(i) {
  return Object.fromEntries(Object.entries(i).filter((t) => t[1] !== void 0));
}
function Y(i, t) {
  const e = N(i);
  if (!e)
    throw new Error("Invalid Quill container");
  const s = !t.theme || t.theme === b.DEFAULTS.theme ? U : b.import(`themes/${t.theme}`);
  if (!s)
    throw new Error(`Invalid theme ${t.theme}. Did you register it?`);
  const {
    modules: r,
    ...o
  } = b.DEFAULTS, {
    modules: u,
    ...d
  } = s.DEFAULTS;
  let a = T(t.modules);
  a != null && a.toolbar && a.toolbar.constructor !== Object && (a = {
    ...a,
    toolbar: {
      container: a.toolbar
    }
  });
  const w = M({}, T(r), T(u), a), f = {
    ...o,
    ...O(d),
    ...O(t)
  };
  let L = t.registry;
  return L ? t.formats && y.warn('Ignoring "formats" option because "registry" is specified') : L = t.formats ? J(t.formats, f.registry, y) : f.registry, {
    ...f,
    registry: L,
    container: e,
    theme: s,
    modules: Object.entries(w).reduce((R, P) => {
      let [S, I] = P;
      if (!I)
        return R;
      const C = b.import(`modules/${S}`);
      return C == null ? (y.error(`Cannot load ${S} module. Are you sure you registered it?`), R) : {
        ...R,
        // @ts-expect-error
        [S]: M({}, C.DEFAULTS || {}, I)
      };
    }, {}),
    bounds: N(f.bounds)
  };
}
function h(i, t, e, n) {
  if (!this.isEnabled() && t === l.sources.USER && !this.allowReadOnlyEdits)
    return new c();
  let s = e == null ? null : this.getSelection();
  const r = this.editor.delta, o = i();
  if (s != null && (e === !0 && (e = s.index), n == null ? s = v(s, o, t) : n !== 0 && (s = v(s, e, n, t)), this.setSelection(s, l.sources.SILENT)), o.length() > 0) {
    const u = [l.events.TEXT_CHANGE, o, r, t];
    this.emitter.emit(l.events.EDITOR_CHANGE, ...u), t !== l.sources.SILENT && this.emitter.emit(...u);
  }
  return o;
}
function g(i, t, e, n, s) {
  let r = {};
  return typeof i.index == "number" && typeof i.length == "number" ? typeof t != "number" ? (s = n, n = e, e = t, t = i.length, i = i.index) : (t = i.length, i = i.index) : typeof t != "number" && (s = n, n = e, e = t, t = 0), typeof e == "object" ? (r = e, s = n) : typeof e == "string" && (n != null ? r[e] = n : s = e), s = s || l.sources.API, [i, t, r, s];
}
function v(i, t, e, n) {
  const s = typeof e == "number" ? e : 0;
  if (i == null)
    return null;
  let r, o;
  return t && typeof t.transformPosition == "function" ? [r, o] = [i.index, i.index + i.length].map((u) => (
    // @ts-expect-error -- TODO: add a better type guard around `index`
    t.transformPosition(u, n !== l.sources.USER)
  )) : [r, o] = [i.index, i.index + i.length].map((u) => u < t || u === t && n === l.sources.USER ? u : s >= 0 ? u + s : Math.max(t, u + s)), new B(r, o - r);
}
export {
  V as Parchment,
  B as Range,
  b as default,
  Y as expandConfig,
  E as globalRegistry,
  g as overload
};
