import { N as I, f as S, R as $, S as x, F as u, l as w, c as R, a as L } from "./index-DU6X3Zlf.js";
function b(d, t = null) {
  return function(e, n) {
    let { $from: o, $to: l } = e.selection, f = o.blockRange(l);
    if (!f)
      return !1;
    let i = n ? e.tr : null;
    return v(i, f, d, t) ? (n && n(i.scrollIntoView()), !0) : !1;
  };
}
function v(d, t, e, n = null) {
  let o = !1, l = t, f = t.$from.doc;
  if (t.depth >= 2 && t.$from.node(t.depth - 1).type.compatibleContent(e) && t.startIndex == 0) {
    if (t.$from.index(t.depth - 1) == 0)
      return !1;
    let r = f.resolve(t.start - 2);
    l = new I(r, r, t.depth), t.endIndex < t.parent.childCount && (t = new I(t.$from, f.resolve(t.$to.end(t.depth)), t.depth)), o = !0;
  }
  let i = S(l, e, n, t);
  return i ? (d && C(d, t, i, o, e), !0) : !1;
}
function C(d, t, e, n, o) {
  let l = u.empty;
  for (let s = e.length - 1; s >= 0; s--)
    l = u.from(e[s].type.create(e[s].attrs, l));
  d.step(new $(t.start - (n ? 2 : 0), t.end, t.start, t.end, new x(l, 0, 0), e.length, !0));
  let f = 0;
  for (let s = 0; s < e.length; s++)
    e[s].type == o && (f = s + 1);
  let i = e.length - f, r = t.start + e.length - (n ? 2 : 0), m = t.parent;
  for (let s = t.startIndex, p = t.endIndex, c = !0; s < p; s++, c = !1)
    !c && L(d.doc, r, i) && (d.split(r, i), r += 2 * i), r += m.child(s).nodeSize;
  return d;
}
function V(d) {
  return function(t, e) {
    let { $from: n, $to: o } = t.selection, l = n.blockRange(o, (f) => f.childCount > 0 && f.firstChild.type == d);
    return l ? e ? n.node(l.depth - 1).type == d ? z(t, e, d, l) : A(t, e, l) : !0 : !1;
  };
}
function z(d, t, e, n) {
  let o = d.tr, l = n.end, f = n.$to.end(n.depth);
  l < f && (o.step(new $(l - 1, f, l, f, new x(u.from(e.create(null, n.parent.copy())), 1, 0), 1, !0)), n = new I(o.doc.resolve(n.$from.pos), o.doc.resolve(f), n.depth));
  const i = w(n);
  if (i == null)
    return !1;
  o.lift(n, i);
  let r = o.doc.resolve(o.mapping.map(l, -1) - 1);
  return R(o.doc, r.pos) && r.nodeBefore.type == r.nodeAfter.type && o.join(r.pos), t(o.scrollIntoView()), !0;
}
function A(d, t, e) {
  let n = d.tr, o = e.parent;
  for (let h = e.end, a = e.endIndex - 1, y = e.startIndex; a > y; a--)
    h -= o.child(a).nodeSize, n.delete(h - 1, h + 1);
  let l = n.doc.resolve(e.start), f = l.nodeAfter;
  if (n.mapping.map(e.end) != e.start + l.nodeAfter.nodeSize)
    return !1;
  let i = e.startIndex == 0, r = e.endIndex == o.childCount, m = l.node(-1), s = l.index(-1);
  if (!m.canReplace(s + (i ? 0 : 1), s + 1, f.content.append(r ? u.empty : u.from(o))))
    return !1;
  let p = l.pos, c = p + f.nodeSize;
  return n.step(new $(p - (i ? 1 : 0), c + (r ? 1 : 0), p + 1, c - 1, new x((i ? u.empty : u.from(o.copy(u.empty))).append(r ? u.empty : u.from(o.copy(u.empty))), i ? 0 : 1, r ? 0 : 1), i ? 0 : 1)), t(n.scrollIntoView()), !0;
}
export {
  V as liftListItem,
  b as wrapInList,
  v as wrapRangeInList
};
