var q = Object.defineProperty;
var b = (e, t, i) => t in e ? q(e, t, { enumerable: !0, configurable: !0, writable: !0, value: i }) : e[t] = i;
var c = (e, t, i) => (b(e, typeof t != "symbol" ? t + "" : t, i), i);
import n from "../core/emitter.mjs";
import a, { BaseTooltip as E } from "./base.mjs";
import { Range as L } from "../core/selection.mjs";
import d from "../ui/icons.mjs";
import x from "../core/quill.mjs";
import g from "../../../../lodash-es@4.17.21/node_modules/lodash-es/merge.mjs";
const T = [["bold", "italic", "link"], [{
  header: 1
}, {
  header: 2
}, "blockquote"]];
class m extends E {
  constructor(t, i) {
    super(t, i), this.quill.on(n.events.EDITOR_CHANGE, (l, s, y, f) => {
      if (l === n.events.SELECTION_CHANGE)
        if (s != null && s.length > 0 && f === n.sources.USER) {
          this.show(), this.root.style.left = "0px", this.root.style.width = "", this.root.style.width = `${this.root.offsetWidth}px`;
          const r = this.quill.getLines(s.index, s.length);
          if (r.length === 1) {
            const o = this.quill.getBounds(s);
            o != null && this.position(o);
          } else {
            const o = r[r.length - 1], u = this.quill.getIndex(o), p = Math.min(o.length() - 1, s.index + s.length - u), h = this.quill.getBounds(new L(u, p));
            h != null && this.position(h);
          }
        } else
          document.activeElement !== this.textbox && this.quill.hasFocus() && this.hide();
    });
  }
  listen() {
    super.listen(), this.root.querySelector(".ql-close").addEventListener("click", () => {
      this.root.classList.remove("ql-editing");
    }), this.quill.on(n.events.SCROLL_OPTIMIZE, () => {
      setTimeout(() => {
        if (this.root.classList.contains("ql-hidden"))
          return;
        const t = this.quill.getSelection();
        if (t != null) {
          const i = this.quill.getBounds(t);
          i != null && this.position(i);
        }
      }, 1);
    });
  }
  cancel() {
    this.show();
  }
  position(t) {
    const i = super.position(t), l = this.root.querySelector(".ql-tooltip-arrow");
    return l.style.marginLeft = "", i !== 0 && (l.style.marginLeft = `${-1 * i - l.offsetWidth / 2}px`), i;
  }
}
c(m, "TEMPLATE", ['<span class="ql-tooltip-arrow"></span>', '<div class="ql-tooltip-editor">', '<input type="text" data-formula="e=mc^2" data-link="https://quilljs.com" data-video="Embed URL">', '<a class="ql-close"></a>', "</div>"].join(""));
class S extends a {
  constructor(t, i) {
    i.modules.toolbar != null && i.modules.toolbar.container == null && (i.modules.toolbar.container = T), super(t, i), this.quill.container.classList.add("ql-bubble");
  }
  extendToolbar(t) {
    this.tooltip = new m(this.quill, this.options.bounds), t.container != null && (this.tooltip.root.appendChild(t.container), this.buildButtons(t.container.querySelectorAll("button"), d), this.buildPickers(t.container.querySelectorAll("select"), d));
  }
}
S.DEFAULTS = g({}, a.DEFAULTS, {
  modules: {
    toolbar: {
      handlers: {
        link(e) {
          e ? this.quill.theme.tooltip.edit() : this.quill.format("link", !1, x.sources.USER);
        }
      }
    }
  }
});
export {
  m as BubbleTooltip,
  S as default
};
