import f from "../../../../quill-delta@5.1.0/node_modules/quill-delta/dist/Delta.mjs";
import { Scope as d, EmbedBlot as m } from "../../../../parchment@3.0.0/node_modules/parchment/dist/parchment.mjs";
import a from "../core/quill.mjs";
import g from "../core/logger.mjs";
import p from "../core/module.mjs";
const c = g("quill:toolbar");
class h extends p {
  constructor(t, s) {
    var l, n;
    if (super(t, s), Array.isArray(this.options.container)) {
      const e = document.createElement("div");
      e.setAttribute("role", "toolbar"), q(e, this.options.container), (n = (l = t.container) == null ? void 0 : l.parentNode) == null || n.insertBefore(e, t.container), this.container = e;
    } else
      typeof this.options.container == "string" ? this.container = document.querySelector(this.options.container) : this.container = this.options.container;
    if (!(this.container instanceof HTMLElement)) {
      c.error("Container required for toolbar", this.options);
      return;
    }
    this.container.classList.add("ql-toolbar"), this.controls = [], this.handlers = {}, this.options.handlers && Object.keys(this.options.handlers).forEach((e) => {
      var o;
      const r = (o = this.options.handlers) == null ? void 0 : o[e];
      r && this.addHandler(e, r);
    }), Array.from(this.container.querySelectorAll("button, select")).forEach((e) => {
      this.attach(e);
    }), this.quill.on(a.events.EDITOR_CHANGE, () => {
      const [e] = this.quill.selection.getRange();
      this.update(e);
    });
  }
  addHandler(t, s) {
    this.handlers[t] = s;
  }
  attach(t) {
    let s = Array.from(t.classList).find((n) => n.indexOf("ql-") === 0);
    if (!s)
      return;
    if (s = s.slice(3), t.tagName === "BUTTON" && t.setAttribute("type", "button"), this.handlers[s] == null && this.quill.scroll.query(s) == null) {
      c.warn("ignoring attaching to nonexistent format", s, t);
      return;
    }
    const l = t.tagName === "SELECT" ? "change" : "click";
    t.addEventListener(l, (n) => {
      let e;
      if (t.tagName === "SELECT") {
        if (t.selectedIndex < 0)
          return;
        const o = t.options[t.selectedIndex];
        o.hasAttribute("selected") ? e = !1 : e = o.value || !1;
      } else
        t.classList.contains("ql-active") ? e = !1 : e = t.value || !t.hasAttribute("value"), n.preventDefault();
      this.quill.focus();
      const [r] = this.quill.selection.getRange();
      if (this.handlers[s] != null)
        this.handlers[s].call(this, e);
      else if (
        // @ts-expect-error
        this.quill.scroll.query(s).prototype instanceof m
      ) {
        if (e = prompt(`Enter ${s}`), !e)
          return;
        this.quill.updateContents(new f().retain(r.index).delete(r.length).insert({
          [s]: e
        }), a.sources.USER);
      } else
        this.quill.format(s, e, a.sources.USER);
      this.update(r);
    }), this.controls.push([s, t]);
  }
  update(t) {
    const s = t == null ? {} : this.quill.getFormat(t);
    this.controls.forEach((l) => {
      const [n, e] = l;
      if (e.tagName === "SELECT") {
        let r = null;
        if (t == null)
          r = null;
        else if (s[n] == null)
          r = e.querySelector("option[selected]");
        else if (!Array.isArray(s[n])) {
          let o = s[n];
          typeof o == "string" && (o = o.replace(/"/g, '\\"')), r = e.querySelector(`option[value="${o}"]`);
        }
        r == null ? (e.value = "", e.selectedIndex = -1) : r.selected = !0;
      } else if (t == null)
        e.classList.remove("ql-active"), e.setAttribute("aria-pressed", "false");
      else if (e.hasAttribute("value")) {
        const r = s[n], o = r === e.getAttribute("value") || r != null && r.toString() === e.getAttribute("value") || r == null && !e.getAttribute("value");
        e.classList.toggle("ql-active", o), e.setAttribute("aria-pressed", o.toString());
      } else {
        const r = s[n] != null;
        e.classList.toggle("ql-active", r), e.setAttribute("aria-pressed", r.toString());
      }
    });
  }
}
h.DEFAULTS = {};
function u(i, t, s) {
  const l = document.createElement("button");
  l.setAttribute("type", "button"), l.classList.add(`ql-${t}`), l.setAttribute("aria-pressed", "false"), s != null ? (l.value = s, l.setAttribute("aria-label", `${t}: ${s}`)) : l.setAttribute("aria-label", t), i.appendChild(l);
}
function q(i, t) {
  Array.isArray(t[0]) || (t = [t]), t.forEach((s) => {
    const l = document.createElement("span");
    l.classList.add("ql-formats"), s.forEach((n) => {
      if (typeof n == "string")
        u(l, n);
      else {
        const e = Object.keys(n)[0], r = n[e];
        Array.isArray(r) ? E(l, e, r) : u(l, e, r);
      }
    }), i.appendChild(l);
  });
}
function E(i, t, s) {
  const l = document.createElement("select");
  l.classList.add(`ql-${t}`), s.forEach((n) => {
    const e = document.createElement("option");
    n !== !1 ? e.setAttribute("value", String(n)) : e.setAttribute("selected", "selected"), l.appendChild(e);
  }), i.appendChild(l);
}
h.DEFAULTS = {
  container: null,
  handlers: {
    clean() {
      const i = this.quill.getSelection();
      if (i != null)
        if (i.length === 0) {
          const t = this.quill.getFormat();
          Object.keys(t).forEach((s) => {
            this.quill.scroll.query(s, d.INLINE) != null && this.quill.format(s, !1, a.sources.USER);
          });
        } else
          this.quill.removeFormat(i.index, i.length, a.sources.USER);
    },
    direction(i) {
      const {
        align: t
      } = this.quill.getFormat();
      i === "rtl" && t == null ? this.quill.format("align", "right", a.sources.USER) : !i && t === "right" && this.quill.format("align", !1, a.sources.USER), this.quill.format("direction", i, a.sources.USER);
    },
    indent(i) {
      const t = this.quill.getSelection(), s = this.quill.getFormat(t), l = parseInt(s.indent || 0, 10);
      if (i === "+1" || i === "-1") {
        let n = i === "+1" ? 1 : -1;
        s.direction === "rtl" && (n *= -1), this.quill.format("indent", l + n, a.sources.USER);
      }
    },
    link(i) {
      i === !0 && (i = prompt("Enter link URL:")), this.quill.format("link", i, a.sources.USER);
    },
    list(i) {
      const t = this.quill.getSelection(), s = this.quill.getFormat(t);
      i === "check" ? s.list === "checked" || s.list === "unchecked" ? this.quill.format("list", !1, a.sources.USER) : this.quill.format("list", "unchecked", a.sources.USER) : this.quill.format("list", i, a.sources.USER);
    }
  }
};
export {
  q as addControls,
  h as default
};
