var q = Object.defineProperty;
var p = (i, t, e) => t in i ? q(i, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : i[t] = e;
var n = (i, t, e) => (p(i, typeof t != "symbol" ? t + "" : t, e), e);
import o from "../core/emitter.mjs";
import d, { BaseTooltip as k } from "./base.mjs";
import u from "../formats/link.mjs";
import { Range as g } from "../core/selection.mjs";
import c from "../ui/icons.mjs";
import v from "../core/quill.mjs";
import S from "../../../../lodash-es@4.17.21/node_modules/lodash-es/merge.mjs";
const w = [[{
  header: ["1", "2", "3", !1]
}], ["bold", "italic", "underline", "link"], [{
  list: "ordered"
}, {
  list: "bullet"
}], ["clean"]];
class h extends k {
  constructor() {
    super(...arguments);
    n(this, "preview", this.root.querySelector("a.ql-preview"));
  }
  listen() {
    super.listen(), this.root.querySelector("a.ql-action").addEventListener("click", (e) => {
      this.root.classList.contains("ql-editing") ? this.save() : this.edit("link", this.preview.textContent), e.preventDefault();
    }), this.root.querySelector("a.ql-remove").addEventListener("click", (e) => {
      if (this.linkRange != null) {
        const l = this.linkRange;
        this.restoreFocus(), this.quill.formatText(l, "link", !1, o.sources.USER), delete this.linkRange;
      }
      e.preventDefault(), this.hide();
    }), this.quill.on(o.events.SELECTION_CHANGE, (e, l, m) => {
      if (e != null) {
        if (e.length === 0 && m === o.sources.USER) {
          const [s, f] = this.quill.scroll.descendant(u, e.index);
          if (s != null) {
            this.linkRange = new g(e.index - f, s.length());
            const r = u.formats(s.domNode);
            this.preview.textContent = r, this.preview.setAttribute("href", r), this.show();
            const a = this.quill.getBounds(this.linkRange);
            a != null && this.position(a);
            return;
          }
        } else
          delete this.linkRange;
        this.hide();
      }
    });
  }
  show() {
    super.show(), this.root.removeAttribute("data-mode");
  }
}
n(h, "TEMPLATE", ['<a class="ql-preview" rel="noopener noreferrer" target="_blank" href="about:blank"></a>', '<input type="text" data-formula="e=mc^2" data-link="https://quilljs.com" data-video="Embed URL">', '<a class="ql-action"></a>', '<a class="ql-remove"></a>'].join(""));
class E extends d {
  constructor(t, e) {
    e.modules.toolbar != null && e.modules.toolbar.container == null && (e.modules.toolbar.container = w), super(t, e), this.quill.container.classList.add("ql-snow");
  }
  extendToolbar(t) {
    t.container != null && (t.container.classList.add("ql-snow"), this.buildButtons(t.container.querySelectorAll("button"), c), this.buildPickers(t.container.querySelectorAll("select"), c), this.tooltip = new h(this.quill, this.options.bounds), t.container.querySelector(".ql-link") && this.quill.keyboard.addBinding({
      key: "k",
      shortKey: !0
    }, (e, l) => {
      t.handlers.link.call(t, !l.format.link);
    }));
  }
}
E.DEFAULTS = S({}, d.DEFAULTS, {
  modules: {
    toolbar: {
      handlers: {
        link(i) {
          if (i) {
            const t = this.quill.getSelection();
            if (t == null || t.length === 0)
              return;
            let e = this.quill.getText(t);
            /^\S+@\S+\.\S+$/.test(e) && e.indexOf("mailto:") !== 0 && (e = `mailto:${e}`);
            const {
              tooltip: l
            } = this.quill.theme;
            l.edit("link", e);
          } else
            this.quill.format("link", !1, v.sources.USER);
        }
      }
    }
  }
});
export {
  E as default
};
