var d = Object.defineProperty;
var u = (t, n, e) => n in t ? d(t, n, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[n] = e;
var s = (t, n, e) => (u(t, typeof n != "symbol" ? n + "" : n, e), e);
import { ParentBlot as l } from "../../../../parchment@3.0.0/node_modules/parchment/dist/parchment.mjs";
import c from "../core/module.mjs";
import h from "../core/quill.mjs";
const f = /Mac/i.test(navigator.platform), g = 100, y = (t) => !!(t.key === "ArrowLeft" || t.key === "ArrowRight" || // RTL scripts or moving from the end of the previous line
t.key === "ArrowUp" || t.key === "ArrowDown" || t.key === "Home" || f && t.key === "a" && t.ctrlKey === !0);
class C extends c {
  constructor(e, r) {
    super(e, r);
    s(this, "isListening", !1);
    s(this, "selectionChangeDeadline", 0);
    this.handleArrowKeys(), this.handleNavigationShortcuts();
  }
  handleArrowKeys() {
    this.quill.keyboard.addBinding({
      key: ["ArrowLeft", "ArrowRight"],
      offset: 0,
      shiftKey: null,
      handler(e, r) {
        let {
          line: i,
          event: o
        } = r;
        if (!(i instanceof l) || !i.uiNode)
          return !0;
        const a = getComputedStyle(i.domNode).direction === "rtl";
        return a && o.key !== "ArrowRight" || !a && o.key !== "ArrowLeft" ? !0 : (this.quill.setSelection(e.index - 1, e.length + (o.shiftKey ? 1 : 0), h.sources.USER), !1);
      }
    });
  }
  handleNavigationShortcuts() {
    this.quill.root.addEventListener("keydown", (e) => {
      !e.defaultPrevented && y(e) && this.ensureListeningToSelectionChange();
    });
  }
  /**
   * We only listen to the `selectionchange` event when
   * there is an intention of moving the caret to the beginning using shortcuts.
   * This is primarily implemented to prevent infinite loops, as we are changing
   * the selection within the handler of a `selectionchange` event.
   */
  ensureListeningToSelectionChange() {
    if (this.selectionChangeDeadline = Date.now() + g, this.isListening)
      return;
    this.isListening = !0;
    const e = () => {
      this.isListening = !1, Date.now() <= this.selectionChangeDeadline && this.handleSelectionChange();
    };
    document.addEventListener("selectionchange", e, {
      once: !0
    });
  }
  handleSelectionChange() {
    const e = document.getSelection();
    if (!e)
      return;
    const r = e.getRangeAt(0);
    if (r.collapsed !== !0 || r.startOffset !== 0)
      return;
    const i = this.quill.scroll.find(r.startContainer);
    if (!(i instanceof l) || !i.uiNode)
      return;
    const o = document.createRange();
    o.setStartAfter(i.uiNode), o.setEndAfter(i.uiNode), e.removeAllRanges(), e.addRange(o);
  }
}
export {
  g as TTL_FOR_VALID_SELECTION_CHANGE,
  C as default
};
