import a from "../../../../quill-delta@5.1.0/node_modules/quill-delta/dist/Delta.mjs";
import o from "../core/module.mjs";
import r from "../core/quill.mjs";
import { deleteRange as u } from "./keyboard.mjs";
const d = ["insertText", "insertReplacementText"];
class h extends o {
  constructor(t, e) {
    super(t, e), t.root.addEventListener("beforeinput", (n) => {
      this.handleBeforeInput(n);
    }), /Android/i.test(navigator.userAgent) || t.on(r.events.COMPOSITION_BEFORE_START, () => {
      this.handleCompositionStart();
    });
  }
  deleteRange(t) {
    u({
      range: t,
      quill: this.quill
    });
  }
  replaceText(t) {
    let e = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "";
    if (t.length === 0)
      return !1;
    if (e) {
      const n = this.quill.getFormat(t.index, 1);
      this.deleteRange(t), this.quill.updateContents(new a().retain(t.index).insert(e, n), r.sources.USER);
    } else
      this.deleteRange(t);
    return this.quill.setSelection(t.index + e.length, 0, r.sources.SILENT), !0;
  }
  handleBeforeInput(t) {
    if (this.quill.composition.isComposing || t.defaultPrevented || !d.includes(t.inputType))
      return;
    const e = t.getTargetRanges ? t.getTargetRanges()[0] : null;
    if (!e || e.collapsed === !0)
      return;
    const n = p(t);
    if (n == null)
      return;
    const s = this.quill.selection.normalizeNative(e), l = s ? this.quill.selection.normalizedToRange(s) : null;
    l && this.replaceText(l, n) && t.preventDefault();
  }
  handleCompositionStart() {
    const t = this.quill.getSelection();
    t && this.replaceText(t);
  }
}
function p(i) {
  var t;
  return typeof i.data == "string" ? i.data : (t = i.dataTransfer) != null && t.types.includes("text/plain") ? i.dataTransfer.getData("text/plain") : null;
}
export {
  h as default
};
