import { keydownHandler as v } from "../../../../prosemirror-keymap@1.2.3/node_modules/prosemirror-keymap/dist/index.js";
import { Plugin as w, TextSelection as m, NodeSelection as x, Selection as u } from "../../../../prosemirror-state@1.4.3/node_modules/prosemirror-state/dist/index.js";
import { Fragment as h, Slice as g } from "../../../../prosemirror-model@1.25.1/node_modules/prosemirror-model/dist/index.js";
import { DecorationSet as y, Decoration as A } from "../../../../prosemirror-view@1.40.0/node_modules/prosemirror-view/dist/index.js";
class i extends u {
  /**
  Create a gap cursor.
  */
  constructor(e) {
    super(e, e);
  }
  map(e, t) {
    let o = e.resolve(t.map(this.head));
    return i.valid(o) ? new i(o) : u.near(o);
  }
  content() {
    return g.empty;
  }
  eq(e) {
    return e instanceof i && e.head == this.head;
  }
  toJSON() {
    return { type: "gapcursor", pos: this.head };
  }
  /**
  @internal
  */
  static fromJSON(e, t) {
    if (typeof t.pos != "number")
      throw new RangeError("Invalid input for GapCursor.fromJSON");
    return new i(e.resolve(t.pos));
  }
  /**
  @internal
  */
  getBookmark() {
    return new d(this.anchor);
  }
  /**
  @internal
  */
  static valid(e) {
    let t = e.parent;
    if (t.isTextblock || !C(e) || !S(e))
      return !1;
    let o = t.type.spec.allowGapCursor;
    if (o != null)
      return o;
    let r = t.contentMatchAt(e.index()).defaultType;
    return r && r.isTextblock;
  }
  /**
  @internal
  */
  static findGapCursorFrom(e, t, o = !1) {
    e: for (; ; ) {
      if (!o && i.valid(e))
        return e;
      let r = e.pos, s = null;
      for (let l = e.depth; ; l--) {
        let f = e.node(l);
        if (t > 0 ? e.indexAfter(l) < f.childCount : e.index(l) > 0) {
          s = f.child(t > 0 ? e.indexAfter(l) : e.index(l) - 1);
          break;
        } else if (l == 0)
          return null;
        r += t;
        let a = e.doc.resolve(r);
        if (i.valid(a))
          return a;
      }
      for (; ; ) {
        let l = t > 0 ? s.firstChild : s.lastChild;
        if (!l) {
          if (s.isAtom && !s.isText && !x.isSelectable(s)) {
            e = e.doc.resolve(r + s.nodeSize * t), o = !1;
            continue e;
          }
          break;
        }
        s = l, r += t;
        let f = e.doc.resolve(r);
        if (i.valid(f))
          return f;
      }
      return null;
    }
  }
}
i.prototype.visible = !1;
i.findFrom = i.findGapCursorFrom;
u.jsonID("gapcursor", i);
class d {
  constructor(e) {
    this.pos = e;
  }
  map(e) {
    return new d(e.map(this.pos));
  }
  resolve(e) {
    let t = e.resolve(this.pos);
    return i.valid(t) ? new i(t) : u.near(t);
  }
}
function C(n) {
  for (let e = n.depth; e >= 0; e--) {
    let t = n.index(e), o = n.node(e);
    if (t == 0) {
      if (o.type.spec.isolating)
        return !0;
      continue;
    }
    for (let r = o.child(t - 1); ; r = r.lastChild) {
      if (r.childCount == 0 && !r.inlineContent || r.isAtom || r.type.spec.isolating)
        return !0;
      if (r.inlineContent)
        return !1;
    }
  }
  return !0;
}
function S(n) {
  for (let e = n.depth; e >= 0; e--) {
    let t = n.indexAfter(e), o = n.node(e);
    if (t == o.childCount) {
      if (o.type.spec.isolating)
        return !0;
      continue;
    }
    for (let r = o.child(t); ; r = r.firstChild) {
      if (r.childCount == 0 && !r.inlineContent || r.isAtom || r.type.spec.isolating)
        return !0;
      if (r.inlineContent)
        return !1;
    }
  }
  return !0;
}
function z() {
  return new w({
    props: {
      decorations: D,
      createSelectionBetween(n, e, t) {
        return e.pos == t.pos && i.valid(t) ? new i(t) : null;
      },
      handleClick: k,
      handleKeyDown: b,
      handleDOMEvents: { beforeinput: T }
    }
  });
}
const b = v({
  ArrowLeft: c("horiz", -1),
  ArrowRight: c("horiz", 1),
  ArrowUp: c("vert", -1),
  ArrowDown: c("vert", 1)
});
function c(n, e) {
  const t = n == "vert" ? e > 0 ? "down" : "up" : e > 0 ? "right" : "left";
  return function(o, r, s) {
    let l = o.selection, f = e > 0 ? l.$to : l.$from, a = l.empty;
    if (l instanceof m) {
      if (!s.endOfTextblock(t) || f.depth == 0)
        return !1;
      a = !1, f = o.doc.resolve(e > 0 ? f.after() : f.before());
    }
    let p = i.findGapCursorFrom(f, e, a);
    return p ? (r && r(o.tr.setSelection(new i(p))), !0) : !1;
  };
}
function k(n, e, t) {
  if (!n || !n.editable)
    return !1;
  let o = n.state.doc.resolve(e);
  if (!i.valid(o))
    return !1;
  let r = n.posAtCoords({ left: t.clientX, top: t.clientY });
  return r && r.inside > -1 && x.isSelectable(n.state.doc.nodeAt(r.inside)) ? !1 : (n.dispatch(n.state.tr.setSelection(new i(o))), !0);
}
function T(n, e) {
  if (e.inputType != "insertCompositionText" || !(n.state.selection instanceof i))
    return !1;
  let { $from: t } = n.state.selection, o = t.parent.contentMatchAt(t.index()).findWrapping(n.state.schema.nodes.text);
  if (!o)
    return !1;
  let r = h.empty;
  for (let l = o.length - 1; l >= 0; l--)
    r = h.from(o[l].createAndFill(null, r));
  let s = n.state.tr.replace(t.pos, t.pos, new g(r, 0, 0));
  return s.setSelection(m.near(s.doc.resolve(t.pos + 1))), n.dispatch(s), !1;
}
function D(n) {
  if (!(n.selection instanceof i))
    return null;
  let e = document.createElement("div");
  return e.className = "ProseMirror-gapcursor", y.create(n.doc, [A.widget(n.selection.head, e, { key: "gapcursor" })]);
}
export {
  i as GapCursor,
  z as gapCursor
};
//# sourceMappingURL=index.js.map
