import r from "../core/emitter.mjs";
import c from "../core/theme.mjs";
import u from "../ui/color-picker.mjs";
import f from "../ui/icon-picker.mjs";
import h from "../ui/picker.mjs";
import d from "../ui/tooltip.mjs";
import m from "../../../../lodash-es@4.17.21/node_modules/lodash-es/merge.mjs";
const p = [!1, "center", "right", "justify"], b = ["#000000", "#e60000", "#ff9900", "#ffff00", "#008a00", "#0066cc", "#9933ff", "#ffffff", "#facccc", "#ffebcc", "#ffffcc", "#cce8cc", "#cce0f5", "#ebd6ff", "#bbbbbb", "#f06666", "#ffc266", "#ffff66", "#66b966", "#66a3e0", "#c285ff", "#888888", "#a10000", "#b26b00", "#b2b200", "#006100", "#0047b2", "#6b24b2", "#444444", "#5c0000", "#663d00", "#666600", "#003700", "#002966", "#3d1466"], g = [!1, "serif", "monospace"], q = ["1", "2", "3", !1], x = ["small", !1, "large", "huge"];
class k extends c {
  constructor(t, e) {
    super(t, e);
    const o = (i) => {
      if (!document.body.contains(t.root)) {
        document.body.removeEventListener("click", o);
        return;
      }
      this.tooltip != null && // @ts-expect-error
      !this.tooltip.root.contains(i.target) && // @ts-expect-error
      document.activeElement !== this.tooltip.textbox && !this.quill.hasFocus() && this.tooltip.hide(), this.pickers != null && this.pickers.forEach((l) => {
        l.container.contains(i.target) || l.close();
      });
    };
    t.emitter.listenDOM("click", document.body, o);
  }
  addModule(t) {
    const e = super.addModule(t);
    return t === "toolbar" && this.extendToolbar(e), e;
  }
  buildButtons(t, e) {
    Array.from(t).forEach((o) => {
      (o.getAttribute("class") || "").split(/\s+/).forEach((l) => {
        if (l.startsWith("ql-") && (l = l.slice(3), e[l] != null))
          if (l === "direction")
            o.innerHTML = e[l][""] + e[l].rtl;
          else if (typeof e[l] == "string")
            o.innerHTML = e[l];
          else {
            const a = o.value || "";
            a != null && e[l][a] && (o.innerHTML = e[l][a]);
          }
      });
    });
  }
  buildPickers(t, e) {
    this.pickers = Array.from(t).map((i) => {
      if (i.classList.contains("ql-align") && (i.querySelector("option") == null && n(i, p), typeof e.align == "object"))
        return new f(i, e.align);
      if (i.classList.contains("ql-background") || i.classList.contains("ql-color")) {
        const l = i.classList.contains("ql-background") ? "background" : "color";
        return i.querySelector("option") == null && n(i, b, l === "background" ? "#ffffff" : "#000000"), new u(i, e[l]);
      }
      return i.querySelector("option") == null && (i.classList.contains("ql-font") ? n(i, g) : i.classList.contains("ql-header") ? n(i, q) : i.classList.contains("ql-size") && n(i, x)), new h(i);
    });
    const o = () => {
      this.pickers.forEach((i) => {
        i.update();
      });
    };
    this.quill.on(r.events.EDITOR_CHANGE, o);
  }
}
k.DEFAULTS = m({}, c.DEFAULTS, {
  modules: {
    toolbar: {
      handlers: {
        formula() {
          this.quill.theme.tooltip.edit("formula");
        },
        image() {
          let s = this.container.querySelector("input.ql-image[type=file]");
          s == null && (s = document.createElement("input"), s.setAttribute("type", "file"), s.setAttribute("accept", this.quill.uploader.options.mimetypes.join(", ")), s.classList.add("ql-image"), s.addEventListener("change", () => {
            const t = this.quill.getSelection(!0);
            this.quill.uploader.upload(t, s.files), s.value = "";
          }), this.container.appendChild(s)), s.click();
        },
        video() {
          this.quill.theme.tooltip.edit("video");
        }
      }
    }
  }
});
class R extends d {
  constructor(t, e) {
    super(t, e), this.textbox = this.root.querySelector('input[type="text"]'), this.listen();
  }
  listen() {
    this.textbox.addEventListener("keydown", (t) => {
      t.key === "Enter" ? (this.save(), t.preventDefault()) : t.key === "Escape" && (this.cancel(), t.preventDefault());
    });
  }
  cancel() {
    this.hide(), this.restoreFocus();
  }
  edit() {
    let t = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : "link", e = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : null;
    if (this.root.classList.remove("ql-hidden"), this.root.classList.add("ql-editing"), this.textbox == null)
      return;
    e != null ? this.textbox.value = e : t !== this.root.getAttribute("data-mode") && (this.textbox.value = "");
    const o = this.quill.getBounds(this.quill.selection.savedRange);
    o != null && this.position(o), this.textbox.select(), this.textbox.setAttribute("placeholder", this.textbox.getAttribute(`data-${t}`) || ""), this.root.setAttribute("data-mode", t);
  }
  restoreFocus() {
    this.quill.focus({
      preventScroll: !0
    });
  }
  save() {
    let {
      value: t
    } = this.textbox;
    switch (this.root.getAttribute("data-mode")) {
      case "link": {
        const {
          scrollTop: e
        } = this.quill.root;
        this.linkRange ? (this.quill.formatText(this.linkRange, "link", t, r.sources.USER), delete this.linkRange) : (this.restoreFocus(), this.quill.format("link", t, r.sources.USER)), this.quill.root.scrollTop = e;
        break;
      }
      case "video":
        t = E(t);
      case "formula": {
        if (!t)
          break;
        const e = this.quill.getSelection(!0);
        if (e != null) {
          const o = e.index + e.length;
          this.quill.insertEmbed(
            o,
            // @ts-expect-error Fix me later
            this.root.getAttribute("data-mode"),
            t,
            r.sources.USER
          ), this.root.getAttribute("data-mode") === "formula" && this.quill.insertText(o + 1, " ", r.sources.USER), this.quill.setSelection(o + 2, r.sources.USER);
        }
        break;
      }
    }
    this.textbox.value = "", this.hide();
  }
}
function E(s) {
  let t = s.match(/^(?:(https?):\/\/)?(?:(?:www|m)\.)?youtube\.com\/watch.*v=([a-zA-Z0-9_-]+)/) || s.match(/^(?:(https?):\/\/)?(?:(?:www|m)\.)?youtu\.be\/([a-zA-Z0-9_-]+)/);
  return t ? `${t[1] || "https"}://www.youtube.com/embed/${t[2]}?showinfo=0` : (t = s.match(/^(?:(https?):\/\/)?(?:www\.)?vimeo\.com\/(\d+)/)) ? `${t[1] || "https"}://player.vimeo.com/video/${t[2]}/` : s;
}
function n(s, t) {
  let e = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : !1;
  t.forEach((o) => {
    const i = document.createElement("option");
    o === e ? i.setAttribute("selected", "selected") : i.setAttribute("value", String(o)), s.appendChild(i);
  });
}
export {
  R as BaseTooltip,
  k as default
};
