import { Plugin as w } from "../../../../prosemirror-state@1.4.3/node_modules/prosemirror-state/dist/index.js";
import { dropPoint as v } from "../../../../prosemirror-transform@1.10.4/node_modules/prosemirror-transform/dist/index.js";
function y(m = {}) {
  return new w({
    view(e) {
      return new C(e, m);
    }
  });
}
class C {
  constructor(e, t) {
    var i;
    this.editorView = e, this.cursorPos = null, this.element = null, this.timeout = -1, this.width = (i = t.width) !== null && i !== void 0 ? i : 1, this.color = t.color === !1 ? void 0 : t.color || "black", this.class = t.class, this.handlers = ["dragover", "dragend", "drop", "dragleave"].map((s) => {
      let d = (r) => {
        this[s](r);
      };
      return e.dom.addEventListener(s, d), { name: s, handler: d };
    });
  }
  destroy() {
    this.handlers.forEach(({ name: e, handler: t }) => this.editorView.dom.removeEventListener(e, t));
  }
  update(e, t) {
    this.cursorPos != null && t.doc != e.state.doc && (this.cursorPos > e.state.doc.content.size ? this.setCursor(null) : this.updateOverlay());
  }
  setCursor(e) {
    e != this.cursorPos && (this.cursorPos = e, e == null ? (this.element.parentNode.removeChild(this.element), this.element = null) : this.updateOverlay());
  }
  updateOverlay() {
    let e = this.editorView.state.doc.resolve(this.cursorPos), t = !e.parent.inlineContent, i, s = this.editorView.dom, d = s.getBoundingClientRect(), r = d.width / s.offsetWidth, h = d.height / s.offsetHeight;
    if (t) {
      let o = e.nodeBefore, n = e.nodeAfter;
      if (o || n) {
        let a = this.editorView.nodeDOM(this.cursorPos - (o ? o.nodeSize : 0));
        if (a) {
          let c = a.getBoundingClientRect(), u = o ? c.bottom : c.top;
          o && n && (u = (u + this.editorView.nodeDOM(this.cursorPos).getBoundingClientRect().top) / 2);
          let g = this.width / 2 * h;
          i = { left: c.left, right: c.right, top: u - g, bottom: u + g };
        }
      }
    }
    if (!i) {
      let o = this.editorView.coordsAtPos(this.cursorPos), n = this.width / 2 * r;
      i = { left: o.left - n, right: o.left + n, top: o.top, bottom: o.bottom };
    }
    let l = this.editorView.dom.offsetParent;
    this.element || (this.element = l.appendChild(document.createElement("div")), this.class && (this.element.className = this.class), this.element.style.cssText = "position: absolute; z-index: 50; pointer-events: none;", this.color && (this.element.style.backgroundColor = this.color)), this.element.classList.toggle("prosemirror-dropcursor-block", t), this.element.classList.toggle("prosemirror-dropcursor-inline", !t);
    let f, p;
    if (!l || l == document.body && getComputedStyle(l).position == "static")
      f = -pageXOffset, p = -pageYOffset;
    else {
      let o = l.getBoundingClientRect(), n = o.width / l.offsetWidth, a = o.height / l.offsetHeight;
      f = o.left - l.scrollLeft * n, p = o.top - l.scrollTop * a;
    }
    this.element.style.left = (i.left - f) / r + "px", this.element.style.top = (i.top - p) / h + "px", this.element.style.width = (i.right - i.left) / r + "px", this.element.style.height = (i.bottom - i.top) / h + "px";
  }
  scheduleRemoval(e) {
    clearTimeout(this.timeout), this.timeout = setTimeout(() => this.setCursor(null), e);
  }
  dragover(e) {
    if (!this.editorView.editable)
      return;
    let t = this.editorView.posAtCoords({ left: e.clientX, top: e.clientY }), i = t && t.inside >= 0 && this.editorView.state.doc.nodeAt(t.inside), s = i && i.type.spec.disableDropCursor, d = typeof s == "function" ? s(this.editorView, t, e) : s;
    if (t && !d) {
      let r = t.pos;
      if (this.editorView.dragging && this.editorView.dragging.slice) {
        let h = v(this.editorView.state.doc, r, this.editorView.dragging.slice);
        h != null && (r = h);
      }
      this.setCursor(r), this.scheduleRemoval(5e3);
    }
  }
  dragend() {
    this.scheduleRemoval(20);
  }
  drop() {
    this.scheduleRemoval(20);
  }
  dragleave(e) {
    this.editorView.dom.contains(e.relatedTarget) || this.setCursor(null);
  }
}
export {
  y as dropCursor
};
//# sourceMappingURL=index.js.map
