import { findWrapping as J, canSplit as h, liftTarget as T, canJoin as A, joinPoint as F, replaceStep as B, ReplaceStep as U, ReplaceAroundStep as v } from "../../../../prosemirror-transform@1.10.4/node_modules/prosemirror-transform/dist/index.js";
import { Slice as y, Fragment as S } from "../../../../prosemirror-model@1.25.1/node_modules/prosemirror-model/dist/index.js";
import { TextSelection as I, NodeSelection as d, Selection as V, AllSelection as P } from "../../../../prosemirror-state@1.4.3/node_modules/prosemirror-state/dist/index.js";
const M = (t, e) => t.selection.empty ? !1 : (e && e(t.tr.deleteSelection().scrollIntoView()), !0);
function N(t, e) {
  let { $cursor: r } = t.selection;
  return !r || (e ? !e.endOfTextblock("backward", t) : r.parentOffset > 0) ? null : r;
}
const G = (t, e, r) => {
  let n = N(t, r);
  if (!n)
    return !1;
  let o = z(n);
  if (!o) {
    let i = n.blockRange(), f = i && T(i);
    return f == null ? !1 : (e && e(t.tr.lift(i, f).scrollIntoView()), !0);
  }
  let l = o.nodeBefore;
  if (D(t, o, e, -1))
    return !0;
  if (n.parent.content.size == 0 && (x(l, "end") || d.isSelectable(l)))
    for (let i = n.depth; ; i--) {
      let f = B(t.doc, n.before(i), n.after(i), y.empty);
      if (f && f.slice.size < f.to - f.from) {
        if (e) {
          let c = t.tr.step(f);
          c.setSelection(x(l, "end") ? V.findFrom(c.doc.resolve(c.mapping.map(o.pos, -1)), -1) : d.create(c.doc, o.pos - l.nodeSize)), e(c.scrollIntoView());
        }
        return !0;
      }
      if (i == 1 || n.node(i - 1).childCount > 1)
        break;
    }
  return l.isAtom && o.depth == n.depth - 1 ? (e && e(t.tr.delete(o.pos - l.nodeSize, o.pos).scrollIntoView()), !0) : !1;
}, oe = (t, e, r) => {
  let n = N(t, r);
  if (!n)
    return !1;
  let o = z(n);
  return o ? E(t, o, e) : !1;
}, le = (t, e, r) => {
  let n = W(t, r);
  if (!n)
    return !1;
  let o = R(n);
  return o ? E(t, o, e) : !1;
};
function E(t, e, r) {
  let n = e.nodeBefore, o = n, l = e.pos - 1;
  for (; !o.isTextblock; l--) {
    if (o.type.spec.isolating)
      return !1;
    let s = o.lastChild;
    if (!s)
      return !1;
    o = s;
  }
  let i = e.nodeAfter, f = i, c = e.pos + 1;
  for (; !f.isTextblock; c++) {
    if (f.type.spec.isolating)
      return !1;
    let s = f.firstChild;
    if (!s)
      return !1;
    f = s;
  }
  let p = B(t.doc, l, c, y.empty);
  if (!p || p.from != l || p instanceof U && p.slice.size >= c - l)
    return !1;
  if (r) {
    let s = t.tr.step(p);
    s.setSelection(I.create(s.doc, l)), r(s.scrollIntoView());
  }
  return !0;
}
function x(t, e, r = !1) {
  for (let n = t; n; n = e == "start" ? n.firstChild : n.lastChild) {
    if (n.isTextblock)
      return !0;
    if (r && n.childCount != 1)
      return !1;
  }
  return !1;
}
const H = (t, e, r) => {
  let { $head: n, empty: o } = t.selection, l = n;
  if (!o)
    return !1;
  if (n.parent.isTextblock) {
    if (r ? !r.endOfTextblock("backward", t) : n.parentOffset > 0)
      return !1;
    l = z(n);
  }
  let i = l && l.nodeBefore;
  return !i || !d.isSelectable(i) ? !1 : (e && e(t.tr.setSelection(d.create(t.doc, l.pos - i.nodeSize)).scrollIntoView()), !0);
};
function z(t) {
  if (!t.parent.type.spec.isolating)
    for (let e = t.depth - 1; e >= 0; e--) {
      if (t.index(e) > 0)
        return t.doc.resolve(t.before(e + 1));
      if (t.node(e).type.spec.isolating)
        break;
    }
  return null;
}
function W(t, e) {
  let { $cursor: r } = t.selection;
  return !r || (e ? !e.endOfTextblock("forward", t) : r.parentOffset < r.parent.content.size) ? null : r;
}
const K = (t, e, r) => {
  let n = W(t, r);
  if (!n)
    return !1;
  let o = R(n);
  if (!o)
    return !1;
  let l = o.nodeAfter;
  if (D(t, o, e, 1))
    return !0;
  if (n.parent.content.size == 0 && (x(l, "start") || d.isSelectable(l))) {
    let i = B(t.doc, n.before(), n.after(), y.empty);
    if (i && i.slice.size < i.to - i.from) {
      if (e) {
        let f = t.tr.step(i);
        f.setSelection(x(l, "start") ? V.findFrom(f.doc.resolve(f.mapping.map(o.pos)), 1) : d.create(f.doc, f.mapping.map(o.pos))), e(f.scrollIntoView());
      }
      return !0;
    }
  }
  return l.isAtom && o.depth == n.depth - 1 ? (e && e(t.tr.delete(o.pos, o.pos + l.nodeSize).scrollIntoView()), !0) : !1;
}, L = (t, e, r) => {
  let { $head: n, empty: o } = t.selection, l = n;
  if (!o)
    return !1;
  if (n.parent.isTextblock) {
    if (r ? !r.endOfTextblock("forward", t) : n.parentOffset < n.parent.content.size)
      return !1;
    l = R(n);
  }
  let i = l && l.nodeAfter;
  return !i || !d.isSelectable(i) ? !1 : (e && e(t.tr.setSelection(d.create(t.doc, l.pos)).scrollIntoView()), !0);
};
function R(t) {
  if (!t.parent.type.spec.isolating)
    for (let e = t.depth - 1; e >= 0; e--) {
      let r = t.node(e);
      if (t.index(e) + 1 < r.childCount)
        return t.doc.resolve(t.after(e + 1));
      if (r.type.spec.isolating)
        break;
    }
  return null;
}
const ie = (t, e) => {
  let r = t.selection, n = r instanceof d, o;
  if (n) {
    if (r.node.isTextblock || !A(t.doc, r.from))
      return !1;
    o = r.from;
  } else if (o = F(t.doc, r.from, -1), o == null)
    return !1;
  if (e) {
    let l = t.tr.join(o);
    n && l.setSelection(d.create(l.doc, o - t.doc.resolve(o).nodeBefore.nodeSize)), e(l.scrollIntoView());
  }
  return !0;
}, fe = (t, e) => {
  let r = t.selection, n;
  if (r instanceof d) {
    if (r.node.isTextblock || !A(t.doc, r.to))
      return !1;
    n = r.to;
  } else if (n = F(t.doc, r.to, 1), n == null)
    return !1;
  return e && e(t.tr.join(n).scrollIntoView()), !0;
}, ce = (t, e) => {
  let { $from: r, $to: n } = t.selection, o = r.blockRange(n), l = o && T(o);
  return l == null ? !1 : (e && e(t.tr.lift(o, l).scrollIntoView()), !0);
}, Q = (t, e) => {
  let { $head: r, $anchor: n } = t.selection;
  return !r.parent.type.spec.code || !r.sameParent(n) ? !1 : (e && e(t.tr.insertText(`
`).scrollIntoView()), !0);
};
function j(t) {
  for (let e = 0; e < t.edgeCount; e++) {
    let { type: r } = t.edge(e);
    if (r.isTextblock && !r.hasRequiredAttrs())
      return r;
  }
  return null;
}
const se = (t, e) => {
  let { $head: r, $anchor: n } = t.selection;
  if (!r.parent.type.spec.code || !r.sameParent(n))
    return !1;
  let o = r.node(-1), l = r.indexAfter(-1), i = j(o.contentMatchAt(l));
  if (!i || !o.canReplaceWith(l, l, i))
    return !1;
  if (e) {
    let f = r.after(), c = t.tr.replaceWith(f, f, i.createAndFill());
    c.setSelection(V.near(c.doc.resolve(f), 1)), e(c.scrollIntoView());
  }
  return !0;
}, X = (t, e) => {
  let r = t.selection, { $from: n, $to: o } = r;
  if (r instanceof P || n.parent.inlineContent || o.parent.inlineContent)
    return !1;
  let l = j(o.parent.contentMatchAt(o.indexAfter()));
  if (!l || !l.isTextblock)
    return !1;
  if (e) {
    let i = (!n.parentOffset && o.index() < o.parent.childCount ? n : o).pos, f = t.tr.insert(i, l.createAndFill());
    f.setSelection(I.create(f.doc, i + 1)), e(f.scrollIntoView());
  }
  return !0;
}, Y = (t, e) => {
  let { $cursor: r } = t.selection;
  if (!r || r.parent.content.size)
    return !1;
  if (r.depth > 1 && r.after() != r.end(-1)) {
    let l = r.before();
    if (h(t.doc, l))
      return e && e(t.tr.split(l).scrollIntoView()), !0;
  }
  let n = r.blockRange(), o = n && T(n);
  return o == null ? !1 : (e && e(t.tr.lift(n, o).scrollIntoView()), !0);
};
function Z(t) {
  return (e, r) => {
    let { $from: n, $to: o } = e.selection;
    if (e.selection instanceof d && e.selection.node.isBlock)
      return !n.parentOffset || !h(e.doc, n.pos) ? !1 : (r && r(e.tr.split(n.pos).scrollIntoView()), !0);
    if (!n.depth)
      return !1;
    let l = [], i, f, c = !1, p = !1;
    for (let u = n.depth; ; u--)
      if (n.node(u).isBlock) {
        c = n.end(u) == n.pos + (n.depth - u), p = n.start(u) == n.pos - (n.depth - u), f = j(n.node(u - 1).contentMatchAt(n.indexAfter(u - 1))), l.unshift(c && f ? { type: f } : null), i = u;
        break;
      } else {
        if (u == 1)
          return !1;
        l.unshift(null);
      }
    let s = e.tr;
    (e.selection instanceof I || e.selection instanceof P) && s.deleteSelection();
    let m = s.mapping.map(n.pos), k = h(s.doc, m, l.length, l);
    if (k || (l[0] = f ? { type: f } : null, k = h(s.doc, m, l.length, l)), !k)
      return !1;
    if (s.split(m, l.length, l), !c && p && n.node(i).type != f) {
      let u = s.mapping.map(n.before(i)), a = s.doc.resolve(u);
      f && n.node(i - 1).canReplaceWith(a.index(), a.index() + 1, f) && s.setNodeMarkup(s.mapping.map(n.before(i)), f);
    }
    return r && r(s.scrollIntoView()), !0;
  };
}
const _ = Z(), ue = (t, e) => {
  let { $from: r, to: n } = t.selection, o, l = r.sharedDepth(n);
  return l == 0 ? !1 : (o = r.before(l), e && e(t.tr.setSelection(d.create(t.doc, o))), !0);
};
function ee(t, e, r) {
  let n = e.nodeBefore, o = e.nodeAfter, l = e.index();
  return !n || !o || !n.type.compatibleContent(o.type) ? !1 : !n.content.size && e.parent.canReplace(l - 1, l) ? (r && r(t.tr.delete(e.pos - n.nodeSize, e.pos).scrollIntoView()), !0) : !e.parent.canReplace(l, l + 1) || !(o.isTextblock || A(t.doc, e.pos)) ? !1 : (r && r(t.tr.join(e.pos).scrollIntoView()), !0);
}
function D(t, e, r, n) {
  let o = e.nodeBefore, l = e.nodeAfter, i, f, c = o.type.spec.isolating || l.type.spec.isolating;
  if (!c && ee(t, e, r))
    return !0;
  let p = !c && e.parent.canReplace(e.index(), e.index() + 1);
  if (p && (i = (f = o.contentMatchAt(o.childCount)).findWrapping(l.type)) && f.matchType(i[0] || l.type).validEnd) {
    if (r) {
      let u = e.pos + l.nodeSize, a = S.empty;
      for (let w = i.length - 1; w >= 0; w--)
        a = S.from(i[w].create(null, a));
      a = S.from(o.copy(a));
      let b = t.tr.step(new v(e.pos - 1, u, e.pos, u, new y(a, 1, 0), i.length, !0)), g = b.doc.resolve(u + 2 * i.length);
      g.nodeAfter && g.nodeAfter.type == o.type && A(b.doc, g.pos) && b.join(g.pos), r(b.scrollIntoView());
    }
    return !0;
  }
  let s = l.type.spec.isolating || n > 0 && c ? null : V.findFrom(e, 1), m = s && s.$from.blockRange(s.$to), k = m && T(m);
  if (k != null && k >= e.depth)
    return r && r(t.tr.lift(m, k).scrollIntoView()), !0;
  if (p && x(l, "start", !0) && x(o, "end")) {
    let u = o, a = [];
    for (; a.push(u), !u.isTextblock; )
      u = u.lastChild;
    let b = l, g = 1;
    for (; !b.isTextblock; b = b.firstChild)
      g++;
    if (u.canReplace(u.childCount, u.childCount, b.content)) {
      if (r) {
        let w = S.empty;
        for (let C = a.length - 1; C >= 0; C--)
          w = S.from(a[C].copy(w));
        let q = t.tr.step(new v(e.pos - a.length, e.pos + l.nodeSize, e.pos + g, e.pos + l.nodeSize - g, new y(w, a.length, 0), 0, !0));
        r(q.scrollIntoView());
      }
      return !0;
    }
  }
  return !1;
}
function $(t) {
  return function(e, r) {
    let n = e.selection, o = t < 0 ? n.$from : n.$to, l = o.depth;
    for (; o.node(l).isInline; ) {
      if (!l)
        return !1;
      l--;
    }
    return o.node(l).isTextblock ? (r && r(e.tr.setSelection(I.create(e.doc, t < 0 ? o.start(l) : o.end(l)))), !0) : !1;
  };
}
const ae = $(-1), pe = $(1);
function de(t, e = null) {
  return function(r, n) {
    let { $from: o, $to: l } = r.selection, i = o.blockRange(l), f = i && J(i, t, e);
    return f ? (n && n(r.tr.wrap(i, f).scrollIntoView()), !0) : !1;
  };
}
function me(t, e = null) {
  return function(r, n) {
    let o = !1;
    for (let l = 0; l < r.selection.ranges.length && !o; l++) {
      let { $from: { pos: i }, $to: { pos: f } } = r.selection.ranges[l];
      r.doc.nodesBetween(i, f, (c, p) => {
        if (o)
          return !1;
        if (!(!c.isTextblock || c.hasMarkup(t, e)))
          if (c.type == t)
            o = !0;
          else {
            let s = r.doc.resolve(p), m = s.index();
            o = s.parent.canReplaceWith(m, m + 1, t);
          }
      });
    }
    if (!o)
      return !1;
    if (n) {
      let l = r.tr;
      for (let i = 0; i < r.selection.ranges.length; i++) {
        let { $from: { pos: f }, $to: { pos: c } } = r.selection.ranges[i];
        l.setBlockType(f, c, t, e);
      }
      n(l.scrollIntoView());
    }
    return !0;
  };
}
function O(...t) {
  return function(e, r, n) {
    for (let o = 0; o < t.length; o++)
      if (t[o](e, r, n))
        return !0;
    return !1;
  };
}
O(M, G, H);
O(M, K, L);
O(Q, X, Y, _);
typeof navigator < "u" ? /Mac|iP(hone|[oa]d)/.test(navigator.platform) : typeof os < "u" && os.platform && os.platform() == "darwin";
export {
  O as chainCommands,
  X as createParagraphNear,
  M as deleteSelection,
  se as exitCode,
  G as joinBackward,
  fe as joinDown,
  K as joinForward,
  oe as joinTextblockBackward,
  le as joinTextblockForward,
  ie as joinUp,
  ce as lift,
  Y as liftEmptyBlock,
  Q as newlineInCode,
  H as selectNodeBackward,
  L as selectNodeForward,
  ue as selectParentNode,
  pe as selectTextblockEnd,
  ae as selectTextblockStart,
  me as setBlockType,
  _ as splitBlock,
  Z as splitBlockAs,
  de as wrapIn
};
//# sourceMappingURL=index.js.map
