import d from "../../../../quill-delta@5.1.0/node_modules/quill-delta/dist/Delta.mjs";
import l from "../core/emitter.mjs";
import c from "../core/module.mjs";
class m extends c {
  constructor(n, s) {
    super(n, s), n.root.addEventListener("drop", (e) => {
      var a;
      e.preventDefault();
      let t = null;
      if (document.caretRangeFromPoint)
        t = document.caretRangeFromPoint(e.clientX, e.clientY);
      else if (document.caretPositionFromPoint) {
        const r = document.caretPositionFromPoint(e.clientX, e.clientY);
        t = document.createRange(), t.setStart(r.offsetNode, r.offset), t.setEnd(r.offsetNode, r.offset);
      }
      const o = t && n.selection.normalizeNative(t);
      if (o) {
        const r = n.selection.normalizedToRange(o);
        (a = e.dataTransfer) != null && a.files && this.upload(r, e.dataTransfer.files);
      }
    });
  }
  upload(n, s) {
    const e = [];
    Array.from(s).forEach((t) => {
      var o;
      t && ((o = this.options.mimetypes) != null && o.includes(t.type)) && e.push(t);
    }), e.length > 0 && this.options.handler.call(this, n, e);
  }
}
m.DEFAULTS = {
  mimetypes: ["image/png", "image/jpeg"],
  handler(i, n) {
    if (!this.quill.scroll.query("image"))
      return;
    const s = n.map((e) => new Promise((t) => {
      const o = new FileReader();
      o.onload = () => {
        t(o.result);
      }, o.readAsDataURL(e);
    }));
    Promise.all(s).then((e) => {
      const t = e.reduce((o, a) => o.insert({
        image: a
      }), new d().retain(i.index).delete(i.length));
      this.quill.updateContents(t, l.sources.USER), this.quill.setSelection(i.index + e.length, l.sources.SILENT);
    });
  }
};
export {
  m as default
};
