var Ve = Object.defineProperty;
var u = (l, e) => Ve(l, "name", { value: e, configurable: !0 });
import { findClusterBreak as Ne } from "../../../../../@marijn_find-cluster-break@1.0.2/node_modules/@marijn/find-cluster-break/src/index.js";
class y {
  static {
    u(this, "Text");
  }
  /**
  Get the line description around the given position.
  */
  lineAt(e) {
    if (e < 0 || e > this.length)
      throw new RangeError(`Invalid position ${e} in document of length ${this.length}`);
    return this.lineInner(e, !1, 1, 0);
  }
  /**
  Get the description for the given (1-based) line number.
  */
  line(e) {
    if (e < 1 || e > this.lines)
      throw new RangeError(`Invalid line number ${e} in ${this.lines}-line document`);
    return this.lineInner(e, !0, 1, 0);
  }
  /**
  Replace a range of the text with the given content.
  */
  replace(e, t, n) {
    [e, t] = L(this, e, t);
    let i = [];
    return this.decompose(
      0,
      e,
      i,
      2
      /* Open.To */
    ), n.length && n.decompose(
      0,
      n.length,
      i,
      3
      /* Open.To */
    ), this.decompose(
      t,
      this.length,
      i,
      1
      /* Open.From */
    ), C.from(i, this.length - (t - e) + n.length);
  }
  /**
  Append another document to this one.
  */
  append(e) {
    return this.replace(this.length, this.length, e);
  }
  /**
  Retrieve the text between the given points.
  */
  slice(e, t = this.length) {
    [e, t] = L(this, e, t);
    let n = [];
    return this.decompose(e, t, n, 0), C.from(n, t - e);
  }
  /**
  Test whether this text is equal to another instance.
  */
  eq(e) {
    if (e == this)
      return !0;
    if (e.length != this.length || e.lines != this.lines)
      return !1;
    let t = this.scanIdentical(e, 1), n = this.length - this.scanIdentical(e, -1), i = new $(this), s = new $(e);
    for (let r = t, h = t; ; ) {
      if (i.next(r), s.next(r), r = 0, i.lineBreak != s.lineBreak || i.done != s.done || i.value != s.value)
        return !1;
      if (h += i.value.length, i.done || h >= n)
        return !0;
    }
  }
  /**
  Iterate over the text. When `dir` is `-1`, iteration happens
  from end to start. This will return lines and the breaks between
  them as separate strings.
  */
  iter(e = 1) {
    return new $(this, e);
  }
  /**
  Iterate over a range of the text. When `from` > `to`, the
  iterator will run in reverse.
  */
  iterRange(e, t = this.length) {
    return new Ie(this, e, t);
  }
  /**
  Return a cursor that iterates over the given range of lines,
  _without_ returning the line breaks between, and yielding empty
  strings for empty lines.
  
  When `from` and `to` are given, they should be 1-based line numbers.
  */
  iterLines(e, t) {
    let n;
    if (e == null)
      n = this.iter();
    else {
      t == null && (t = this.lines + 1);
      let i = this.line(e).from;
      n = this.iterRange(i, Math.max(i, t == this.lines + 1 ? this.length : t <= 1 ? 0 : this.line(t - 1).to));
    }
    return new Pe(n);
  }
  /**
  Return the document as a string, using newline characters to
  separate lines.
  */
  toString() {
    return this.sliceString(0);
  }
  /**
  Convert the document to an array of lines (which can be
  deserialized again via [`Text.of`](https://codemirror.net/6/docs/ref/#state.Text^of)).
  */
  toJSON() {
    let e = [];
    return this.flatten(e), e;
  }
  /**
  @internal
  */
  constructor() {
  }
  /**
  Create a `Text` instance for the given array of lines.
  */
  static of(e) {
    if (e.length == 0)
      throw new RangeError("A document must have at least one line");
    return e.length == 1 && !e[0] ? y.empty : e.length <= 32 ? new w(e) : C.from(w.split(e, []));
  }
}
class w extends y {
  static {
    u(this, "TextLeaf");
  }
  constructor(e, t = De(e)) {
    super(), this.text = e, this.length = t;
  }
  get lines() {
    return this.text.length;
  }
  get children() {
    return null;
  }
  lineInner(e, t, n, i) {
    for (let s = 0; ; s++) {
      let r = this.text[s], h = i + r.length;
      if ((t ? n : h) >= e)
        return new $e(i, h, n, r);
      i = h + 1, n++;
    }
  }
  decompose(e, t, n, i) {
    let s = e <= 0 && t >= this.length ? this : new w(ve(this.text, e, t), Math.min(t, this.length) - Math.max(0, e));
    if (i & 1) {
      let r = n.pop(), h = X(s.text, r.text.slice(), 0, s.length);
      if (h.length <= 32)
        n.push(new w(h, r.length + s.length));
      else {
        let o = h.length >> 1;
        n.push(new w(h.slice(0, o)), new w(h.slice(o)));
      }
    } else
      n.push(s);
  }
  replace(e, t, n) {
    if (!(n instanceof w))
      return super.replace(e, t, n);
    [e, t] = L(this, e, t);
    let i = X(this.text, X(n.text, ve(this.text, 0, e)), t), s = this.length + n.length - (t - e);
    return i.length <= 32 ? new w(i, s) : C.from(w.split(i, []), s);
  }
  sliceString(e, t = this.length, n = `
`) {
    [e, t] = L(this, e, t);
    let i = "";
    for (let s = 0, r = 0; s <= t && r < this.text.length; r++) {
      let h = this.text[r], o = s + h.length;
      s > e && r && (i += n), e < o && t > s && (i += h.slice(Math.max(0, e - s), t - s)), s = o + 1;
    }
    return i;
  }
  flatten(e) {
    for (let t of this.text)
      e.push(t);
  }
  scanIdentical() {
    return 0;
  }
  static split(e, t) {
    let n = [], i = -1;
    for (let s of e)
      n.push(s), i += s.length + 1, n.length == 32 && (t.push(new w(n, i)), n = [], i = -1);
    return i > -1 && t.push(new w(n, i)), t;
  }
}
class C extends y {
  static {
    u(this, "TextNode");
  }
  constructor(e, t) {
    super(), this.children = e, this.length = t, this.lines = 0;
    for (let n of e)
      this.lines += n.lines;
  }
  lineInner(e, t, n, i) {
    for (let s = 0; ; s++) {
      let r = this.children[s], h = i + r.length, o = n + r.lines - 1;
      if ((t ? o : h) >= e)
        return r.lineInner(e, t, n, i);
      i = h + 1, n = o + 1;
    }
  }
  decompose(e, t, n, i) {
    for (let s = 0, r = 0; r <= t && s < this.children.length; s++) {
      let h = this.children[s], o = r + h.length;
      if (e <= o && t >= r) {
        let a = i & ((r <= e ? 1 : 0) | (o >= t ? 2 : 0));
        r >= e && o <= t && !a ? n.push(h) : h.decompose(e - r, t - r, n, a);
      }
      r = o + 1;
    }
  }
  replace(e, t, n) {
    if ([e, t] = L(this, e, t), n.lines < this.lines)
      for (let i = 0, s = 0; i < this.children.length; i++) {
        let r = this.children[i], h = s + r.length;
        if (e >= s && t <= h) {
          let o = r.replace(e - s, t - s, n), a = this.lines - r.lines + o.lines;
          if (o.lines < a >> 4 && o.lines > a >> 6) {
            let f = this.children.slice();
            return f[i] = o, new C(f, this.length - (t - e) + n.length);
          }
          return super.replace(s, h, o);
        }
        s = h + 1;
      }
    return super.replace(e, t, n);
  }
  sliceString(e, t = this.length, n = `
`) {
    [e, t] = L(this, e, t);
    let i = "";
    for (let s = 0, r = 0; s < this.children.length && r <= t; s++) {
      let h = this.children[s], o = r + h.length;
      r > e && s && (i += n), e < o && t > r && (i += h.sliceString(e - r, t - r, n)), r = o + 1;
    }
    return i;
  }
  flatten(e) {
    for (let t of this.children)
      t.flatten(e);
  }
  scanIdentical(e, t) {
    if (!(e instanceof C))
      return 0;
    let n = 0, [i, s, r, h] = t > 0 ? [0, 0, this.children.length, e.children.length] : [this.children.length - 1, e.children.length - 1, -1, -1];
    for (; ; i += t, s += t) {
      if (i == r || s == h)
        return n;
      let o = this.children[i], a = e.children[s];
      if (o != a)
        return n + o.scanIdentical(a, t);
      n += o.length + 1;
    }
  }
  static from(e, t = e.reduce((n, i) => n + i.length + 1, -1)) {
    let n = 0;
    for (let g of e)
      n += g.lines;
    if (n < 32) {
      let g = [];
      for (let m of e)
        m.flatten(g);
      return new w(g, t);
    }
    let i = Math.max(
      32,
      n >> 5
      /* Tree.BranchShift */
    ), s = i << 1, r = i >> 1, h = [], o = 0, a = -1, f = [];
    function c(g) {
      let m;
      if (g.lines > s && g instanceof C)
        for (let O of g.children)
          c(O);
      else g.lines > r && (o > r || !o) ? (d(), h.push(g)) : g instanceof w && o && (m = f[f.length - 1]) instanceof w && g.lines + m.lines <= 32 ? (o += g.lines, a += g.length + 1, f[f.length - 1] = new w(m.text.concat(g.text), m.length + 1 + g.length)) : (o + g.lines > i && d(), o += g.lines, a += g.length + 1, f.push(g));
    }
    u(c, "add");
    function d() {
      o != 0 && (h.push(f.length == 1 ? f[0] : C.from(f, a)), a = -1, o = f.length = 0);
    }
    u(d, "flush");
    for (let g of e)
      c(g);
    return d(), h.length == 1 ? h[0] : new C(h, t);
  }
}
y.empty = /* @__PURE__ */ new w([""], 0);
function De(l) {
  let e = -1;
  for (let t of l)
    e += t.length + 1;
  return e;
}
u(De, "textLength");
function X(l, e, t = 0, n = 1e9) {
  for (let i = 0, s = 0, r = !0; s < l.length && i <= n; s++) {
    let h = l[s], o = i + h.length;
    o >= t && (o > n && (h = h.slice(0, n - i)), i < t && (h = h.slice(t - i)), r ? (e[e.length - 1] += h, r = !1) : e.push(h)), i = o + 1;
  }
  return e;
}
u(X, "appendText");
function ve(l, e, t) {
  return X(l, [""], e, t);
}
u(ve, "sliceText");
class $ {
  static {
    u(this, "RawTextCursor");
  }
  constructor(e, t = 1) {
    this.dir = t, this.done = !1, this.lineBreak = !1, this.value = "", this.nodes = [e], this.offsets = [t > 0 ? 1 : (e instanceof w ? e.text.length : e.children.length) << 1];
  }
  nextInner(e, t) {
    for (this.done = this.lineBreak = !1; ; ) {
      let n = this.nodes.length - 1, i = this.nodes[n], s = this.offsets[n], r = s >> 1, h = i instanceof w ? i.text.length : i.children.length;
      if (r == (t > 0 ? h : 0)) {
        if (n == 0)
          return this.done = !0, this.value = "", this;
        t > 0 && this.offsets[n - 1]++, this.nodes.pop(), this.offsets.pop();
      } else if ((s & 1) == (t > 0 ? 0 : 1)) {
        if (this.offsets[n] += t, e == 0)
          return this.lineBreak = !0, this.value = `
`, this;
        e--;
      } else if (i instanceof w) {
        let o = i.text[r + (t < 0 ? -1 : 0)];
        if (this.offsets[n] += t, o.length > Math.max(0, e))
          return this.value = e == 0 ? o : t > 0 ? o.slice(e) : o.slice(0, o.length - e), this;
        e -= o.length;
      } else {
        let o = i.children[r + (t < 0 ? -1 : 0)];
        e > o.length ? (e -= o.length, this.offsets[n] += t) : (t < 0 && this.offsets[n]--, this.nodes.push(o), this.offsets.push(t > 0 ? 1 : (o instanceof w ? o.text.length : o.children.length) << 1));
      }
    }
  }
  next(e = 0) {
    return e < 0 && (this.nextInner(-e, -this.dir), e = this.value.length), this.nextInner(e, this.dir);
  }
}
class Ie {
  static {
    u(this, "PartialTextCursor");
  }
  constructor(e, t, n) {
    this.value = "", this.done = !1, this.cursor = new $(e, t > n ? -1 : 1), this.pos = t > n ? e.length : 0, this.from = Math.min(t, n), this.to = Math.max(t, n);
  }
  nextInner(e, t) {
    if (t < 0 ? this.pos <= this.from : this.pos >= this.to)
      return this.value = "", this.done = !0, this;
    e += Math.max(0, t < 0 ? this.pos - this.to : this.from - this.pos);
    let n = t < 0 ? this.pos - this.from : this.to - this.pos;
    e > n && (e = n), n -= e;
    let { value: i } = this.cursor.next(e);
    return this.pos += (i.length + e) * t, this.value = i.length <= n ? i : t < 0 ? i.slice(i.length - n) : i.slice(0, n), this.done = !this.value, this;
  }
  next(e = 0) {
    return e < 0 ? e = Math.max(e, this.from - this.pos) : e > 0 && (e = Math.min(e, this.to - this.pos)), this.nextInner(e, this.cursor.dir);
  }
  get lineBreak() {
    return this.cursor.lineBreak && this.value != "";
  }
}
class Pe {
  static {
    u(this, "LineCursor");
  }
  constructor(e) {
    this.inner = e, this.afterBreak = !0, this.value = "", this.done = !1;
  }
  next(e = 0) {
    let { done: t, lineBreak: n, value: i } = this.inner.next(e);
    return t && this.afterBreak ? (this.value = "", this.afterBreak = !1) : t ? (this.done = !0, this.value = "") : n ? this.afterBreak ? this.value = "" : (this.afterBreak = !0, this.next()) : (this.value = i, this.afterBreak = !1), this;
  }
  get lineBreak() {
    return !1;
  }
}
typeof Symbol < "u" && (y.prototype[Symbol.iterator] = function() {
  return this.iter();
}, $.prototype[Symbol.iterator] = Ie.prototype[Symbol.iterator] = Pe.prototype[Symbol.iterator] = function() {
  return this;
});
class $e {
  static {
    u(this, "Line");
  }
  /**
  @internal
  */
  constructor(e, t, n, i) {
    this.from = e, this.to = t, this.number = n, this.text = i;
  }
  /**
  The length of the line (not including any line break after it).
  */
  get length() {
    return this.to - this.from;
  }
}
function L(l, e, t) {
  return e = Math.max(0, Math.min(l.length, e)), [e, Math.max(e, Math.min(l.length, t))];
}
u(L, "clip");
function Z(l, e, t = !0, n = !0) {
  return Ne(l, e, t, n);
}
u(Z, "findClusterBreak");
function qe(l) {
  return l >= 56320 && l < 57344;
}
u(qe, "surrogateLow");
function ze(l) {
  return l >= 55296 && l < 56320;
}
u(ze, "surrogateHigh");
function it(l, e) {
  let t = l.charCodeAt(e);
  if (!ze(t) || e + 1 == l.length)
    return t;
  let n = l.charCodeAt(e + 1);
  return qe(n) ? (t - 55296 << 10) + (n - 56320) + 65536 : t;
}
u(it, "codePointAt");
function st(l) {
  return l <= 65535 ? String.fromCharCode(l) : (l -= 65536, String.fromCharCode((l >> 10) + 55296, (l & 1023) + 56320));
}
u(st, "fromCodePoint");
function rt(l) {
  return l < 65536 ? 1 : 2;
}
u(rt, "codePointSize");
const re = /\r\n?|\n/;
var b = /* @__PURE__ */ function(l) {
  return l[l.Simple = 0] = "Simple", l[l.TrackDel = 1] = "TrackDel", l[l.TrackBefore = 2] = "TrackBefore", l[l.TrackAfter = 3] = "TrackAfter", l;
}(b || (b = {}));
class M {
  static {
    u(this, "ChangeDesc");
  }
  // Sections are encoded as pairs of integers. The first is the
  // length in the current document, and the second is -1 for
  // unaffected sections, and the length of the replacement content
  // otherwise. So an insertion would be (0, n>0), a deletion (n>0,
  // 0), and a replacement two positive numbers.
  /**
  @internal
  */
  constructor(e) {
    this.sections = e;
  }
  /**
  The length of the document before the change.
  */
  get length() {
    let e = 0;
    for (let t = 0; t < this.sections.length; t += 2)
      e += this.sections[t];
    return e;
  }
  /**
  The length of the document after the change.
  */
  get newLength() {
    let e = 0;
    for (let t = 0; t < this.sections.length; t += 2) {
      let n = this.sections[t + 1];
      e += n < 0 ? this.sections[t] : n;
    }
    return e;
  }
  /**
  False when there are actual changes in this set.
  */
  get empty() {
    return this.sections.length == 0 || this.sections.length == 2 && this.sections[1] < 0;
  }
  /**
  Iterate over the unchanged parts left by these changes. `posA`
  provides the position of the range in the old document, `posB`
  the new position in the changed document.
  */
  iterGaps(e) {
    for (let t = 0, n = 0, i = 0; t < this.sections.length; ) {
      let s = this.sections[t++], r = this.sections[t++];
      r < 0 ? (e(n, i, s), i += s) : i += r, n += s;
    }
  }
  /**
  Iterate over the ranges changed by these changes. (See
  [`ChangeSet.iterChanges`](https://codemirror.net/6/docs/ref/#state.ChangeSet.iterChanges) for a
  variant that also provides you with the inserted text.)
  `fromA`/`toA` provides the extent of the change in the starting
  document, `fromB`/`toB` the extent of the replacement in the
  changed document.
  
  When `individual` is true, adjacent changes (which are kept
  separate for [position mapping](https://codemirror.net/6/docs/ref/#state.ChangeDesc.mapPos)) are
  reported separately.
  */
  iterChangedRanges(e, t = !1) {
    le(this, e, t);
  }
  /**
  Get a description of the inverted form of these changes.
  */
  get invertedDesc() {
    let e = [];
    for (let t = 0; t < this.sections.length; ) {
      let n = this.sections[t++], i = this.sections[t++];
      i < 0 ? e.push(n, i) : e.push(i, n);
    }
    return new M(e);
  }
  /**
  Compute the combined effect of applying another set of changes
  after this one. The length of the document after this set should
  match the length before `other`.
  */
  composeDesc(e) {
    return this.empty ? e : e.empty ? this : Ae(this, e);
  }
  /**
  Map this description, which should start with the same document
  as `other`, over another set of changes, so that it can be
  applied after it. When `before` is true, map as if the changes
  in `this` happened before the ones in `other`.
  */
  mapDesc(e, t = !1) {
    return e.empty ? this : he(this, e, t);
  }
  mapPos(e, t = -1, n = b.Simple) {
    let i = 0, s = 0;
    for (let r = 0; r < this.sections.length; ) {
      let h = this.sections[r++], o = this.sections[r++], a = i + h;
      if (o < 0) {
        if (a > e)
          return s + (e - i);
        s += h;
      } else {
        if (n != b.Simple && a >= e && (n == b.TrackDel && i < e && a > e || n == b.TrackBefore && i < e || n == b.TrackAfter && a > e))
          return null;
        if (a > e || a == e && t < 0 && !h)
          return e == i || t < 0 ? s : s + o;
        s += o;
      }
      i = a;
    }
    if (e > i)
      throw new RangeError(`Position ${e} is out of range for changeset of length ${i}`);
    return s;
  }
  /**
  Check whether these changes touch a given range. When one of the
  changes entirely covers the range, the string `"cover"` is
  returned.
  */
  touchesRange(e, t = e) {
    for (let n = 0, i = 0; n < this.sections.length && i <= t; ) {
      let s = this.sections[n++], r = this.sections[n++], h = i + s;
      if (r >= 0 && i <= t && h >= e)
        return i < e && h > t ? "cover" : !0;
      i = h;
    }
    return !1;
  }
  /**
  @internal
  */
  toString() {
    let e = "";
    for (let t = 0; t < this.sections.length; ) {
      let n = this.sections[t++], i = this.sections[t++];
      e += (e ? " " : "") + n + (i >= 0 ? ":" + i : "");
    }
    return e;
  }
  /**
  Serialize this change desc to a JSON-representable value.
  */
  toJSON() {
    return this.sections;
  }
  /**
  Create a change desc from its JSON representation (as produced
  by [`toJSON`](https://codemirror.net/6/docs/ref/#state.ChangeDesc.toJSON).
  */
  static fromJSON(e) {
    if (!Array.isArray(e) || e.length % 2 || e.some((t) => typeof t != "number"))
      throw new RangeError("Invalid JSON representation of ChangeDesc");
    return new M(e);
  }
  /**
  @internal
  */
  static create(e) {
    return new M(e);
  }
}
class k extends M {
  static {
    u(this, "ChangeSet");
  }
  constructor(e, t) {
    super(e), this.inserted = t;
  }
  /**
  Apply the changes to a document, returning the modified
  document.
  */
  apply(e) {
    if (this.length != e.length)
      throw new RangeError("Applying change set to a document with the wrong length");
    return le(this, (t, n, i, s, r) => e = e.replace(i, i + (n - t), r), !1), e;
  }
  mapDesc(e, t = !1) {
    return he(this, e, t, !0);
  }
  /**
  Given the document as it existed _before_ the changes, return a
  change set that represents the inverse of this set, which could
  be used to go from the document created by the changes back to
  the document as it existed before the changes.
  */
  invert(e) {
    let t = this.sections.slice(), n = [];
    for (let i = 0, s = 0; i < t.length; i += 2) {
      let r = t[i], h = t[i + 1];
      if (h >= 0) {
        t[i] = h, t[i + 1] = r;
        let o = i >> 1;
        for (; n.length < o; )
          n.push(y.empty);
        n.push(r ? e.slice(s, s + r) : y.empty);
      }
      s += r;
    }
    return new k(t, n);
  }
  /**
  Combine two subsequent change sets into a single set. `other`
  must start in the document produced by `this`. If `this` goes
  `docA` → `docB` and `other` represents `docB` → `docC`, the
  returned value will represent the change `docA` → `docC`.
  */
  compose(e) {
    return this.empty ? e : e.empty ? this : Ae(this, e, !0);
  }
  /**
  Given another change set starting in the same document, maps this
  change set over the other, producing a new change set that can be
  applied to the document produced by applying `other`. When
  `before` is `true`, order changes as if `this` comes before
  `other`, otherwise (the default) treat `other` as coming first.
  
  Given two changes `A` and `B`, `A.compose(B.map(A))` and
  `B.compose(A.map(B, true))` will produce the same document. This
  provides a basic form of [operational
  transformation](https://en.wikipedia.org/wiki/Operational_transformation),
  and can be used for collaborative editing.
  */
  map(e, t = !1) {
    return e.empty ? this : he(this, e, t, !0);
  }
  /**
  Iterate over the changed ranges in the document, calling `f` for
  each, with the range in the original document (`fromA`-`toA`)
  and the range that replaces it in the new document
  (`fromB`-`toB`).
  
  When `individual` is true, adjacent changes are reported
  separately.
  */
  iterChanges(e, t = !1) {
    le(this, e, t);
  }
  /**
  Get a [change description](https://codemirror.net/6/docs/ref/#state.ChangeDesc) for this change
  set.
  */
  get desc() {
    return M.create(this.sections);
  }
  /**
  @internal
  */
  filter(e) {
    let t = [], n = [], i = [], s = new z(this);
    e: for (let r = 0, h = 0; ; ) {
      let o = r == e.length ? 1e9 : e[r++];
      for (; h < o || h == o && s.len == 0; ) {
        if (s.done)
          break e;
        let f = Math.min(s.len, o - h);
        S(i, f, -1);
        let c = s.ins == -1 ? -1 : s.off == 0 ? s.ins : 0;
        S(t, f, c), c > 0 && R(n, t, s.text), s.forward(f), h += f;
      }
      let a = e[r++];
      for (; h < a; ) {
        if (s.done)
          break e;
        let f = Math.min(s.len, a - h);
        S(t, f, -1), S(i, f, s.ins == -1 ? -1 : s.off == 0 ? s.ins : 0), s.forward(f), h += f;
      }
    }
    return {
      changes: new k(t, n),
      filtered: M.create(i)
    };
  }
  /**
  Serialize this change set to a JSON-representable value.
  */
  toJSON() {
    let e = [];
    for (let t = 0; t < this.sections.length; t += 2) {
      let n = this.sections[t], i = this.sections[t + 1];
      i < 0 ? e.push(n) : i == 0 ? e.push([n]) : e.push([n].concat(this.inserted[t >> 1].toJSON()));
    }
    return e;
  }
  /**
  Create a change set for the given changes, for a document of the
  given length, using `lineSep` as line separator.
  */
  static of(e, t, n) {
    let i = [], s = [], r = 0, h = null;
    function o(f = !1) {
      if (!f && !i.length)
        return;
      r < t && S(i, t - r, -1);
      let c = new k(i, s);
      h = h ? h.compose(c.map(h)) : c, i = [], s = [], r = 0;
    }
    u(o, "flush");
    function a(f) {
      if (Array.isArray(f))
        for (let c of f)
          a(c);
      else if (f instanceof k) {
        if (f.length != t)
          throw new RangeError(`Mismatched change set length (got ${f.length}, expected ${t})`);
        o(), h = h ? h.compose(f.map(h)) : f;
      } else {
        let { from: c, to: d = c, insert: g } = f;
        if (c > d || c < 0 || d > t)
          throw new RangeError(`Invalid change range ${c} to ${d} (in doc of length ${t})`);
        let m = g ? typeof g == "string" ? y.of(g.split(n || re)) : g : y.empty, O = m.length;
        if (c == d && O == 0)
          return;
        c < r && o(), c > r && S(i, c - r, -1), S(i, d - c, O), R(s, i, m), r = d;
      }
    }
    return u(a, "process"), a(e), o(!h), h;
  }
  /**
  Create an empty changeset of the given length.
  */
  static empty(e) {
    return new k(e ? [e, -1] : [], []);
  }
  /**
  Create a changeset from its JSON representation (as produced by
  [`toJSON`](https://codemirror.net/6/docs/ref/#state.ChangeSet.toJSON).
  */
  static fromJSON(e) {
    if (!Array.isArray(e))
      throw new RangeError("Invalid JSON representation of ChangeSet");
    let t = [], n = [];
    for (let i = 0; i < e.length; i++) {
      let s = e[i];
      if (typeof s == "number")
        t.push(s, -1);
      else {
        if (!Array.isArray(s) || typeof s[0] != "number" || s.some((r, h) => h && typeof r != "string"))
          throw new RangeError("Invalid JSON representation of ChangeSet");
        if (s.length == 1)
          t.push(s[0], 0);
        else {
          for (; n.length < i; )
            n.push(y.empty);
          n[i] = y.of(s.slice(1)), t.push(s[0], n[i].length);
        }
      }
    }
    return new k(t, n);
  }
  /**
  @internal
  */
  static createSet(e, t) {
    return new k(e, t);
  }
}
function S(l, e, t, n = !1) {
  if (e == 0 && t <= 0)
    return;
  let i = l.length - 2;
  i >= 0 && t <= 0 && t == l[i + 1] ? l[i] += e : i >= 0 && e == 0 && l[i] == 0 ? l[i + 1] += t : n ? (l[i] += e, l[i + 1] += t) : l.push(e, t);
}
u(S, "addSection");
function R(l, e, t) {
  if (t.length == 0)
    return;
  let n = e.length - 2 >> 1;
  if (n < l.length)
    l[l.length - 1] = l[l.length - 1].append(t);
  else {
    for (; l.length < n; )
      l.push(y.empty);
    l.push(t);
  }
}
u(R, "addInsert");
function le(l, e, t) {
  let n = l.inserted;
  for (let i = 0, s = 0, r = 0; r < l.sections.length; ) {
    let h = l.sections[r++], o = l.sections[r++];
    if (o < 0)
      i += h, s += h;
    else {
      let a = i, f = s, c = y.empty;
      for (; a += h, f += o, o && n && (c = c.append(n[r - 2 >> 1])), !(t || r == l.sections.length || l.sections[r + 1] < 0); )
        h = l.sections[r++], o = l.sections[r++];
      e(i, a, s, f, c), i = a, s = f;
    }
  }
}
u(le, "iterChanges");
function he(l, e, t, n = !1) {
  let i = [], s = n ? [] : null, r = new z(l), h = new z(e);
  for (let o = -1; ; ) {
    if (r.done && h.len || h.done && r.len)
      throw new Error("Mismatched change set lengths");
    if (r.ins == -1 && h.ins == -1) {
      let a = Math.min(r.len, h.len);
      S(i, a, -1), r.forward(a), h.forward(a);
    } else if (h.ins >= 0 && (r.ins < 0 || o == r.i || r.off == 0 && (h.len < r.len || h.len == r.len && !t))) {
      let a = h.len;
      for (S(i, h.ins, -1); a; ) {
        let f = Math.min(r.len, a);
        r.ins >= 0 && o < r.i && r.len <= f && (S(i, 0, r.ins), s && R(s, i, r.text), o = r.i), r.forward(f), a -= f;
      }
      h.next();
    } else if (r.ins >= 0) {
      let a = 0, f = r.len;
      for (; f; )
        if (h.ins == -1) {
          let c = Math.min(f, h.len);
          a += c, f -= c, h.forward(c);
        } else if (h.ins == 0 && h.len < f)
          f -= h.len, h.next();
        else
          break;
      S(i, a, o < r.i ? r.ins : 0), s && o < r.i && R(s, i, r.text), o = r.i, r.forward(r.len - f);
    } else {
      if (r.done && h.done)
        return s ? k.createSet(i, s) : M.create(i);
      throw new Error("Mismatched change set lengths");
    }
  }
}
u(he, "mapSet");
function Ae(l, e, t = !1) {
  let n = [], i = t ? [] : null, s = new z(l), r = new z(e);
  for (let h = !1; ; ) {
    if (s.done && r.done)
      return i ? k.createSet(n, i) : M.create(n);
    if (s.ins == 0)
      S(n, s.len, 0, h), s.next();
    else if (r.len == 0 && !r.done)
      S(n, 0, r.ins, h), i && R(i, n, r.text), r.next();
    else {
      if (s.done || r.done)
        throw new Error("Mismatched change set lengths");
      {
        let o = Math.min(s.len2, r.len), a = n.length;
        if (s.ins == -1) {
          let f = r.ins == -1 ? -1 : r.off ? 0 : r.ins;
          S(n, o, f, h), i && f && R(i, n, r.text);
        } else r.ins == -1 ? (S(n, s.off ? 0 : s.len, o, h), i && R(i, n, s.textBit(o))) : (S(n, s.off ? 0 : s.len, r.off ? 0 : r.ins, h), i && !r.off && R(i, n, r.text));
        h = (s.ins > o || r.ins >= 0 && r.len > o) && (h || n.length > a), s.forward2(o), r.forward(o);
      }
    }
  }
}
u(Ae, "composeSets");
class z {
  static {
    u(this, "SectionIter");
  }
  constructor(e) {
    this.set = e, this.i = 0, this.next();
  }
  next() {
    let { sections: e } = this.set;
    this.i < e.length ? (this.len = e[this.i++], this.ins = e[this.i++]) : (this.len = 0, this.ins = -2), this.off = 0;
  }
  get done() {
    return this.ins == -2;
  }
  get len2() {
    return this.ins < 0 ? this.len : this.ins;
  }
  get text() {
    let { inserted: e } = this.set, t = this.i - 2 >> 1;
    return t >= e.length ? y.empty : e[t];
  }
  textBit(e) {
    let { inserted: t } = this.set, n = this.i - 2 >> 1;
    return n >= t.length && !e ? y.empty : t[n].slice(this.off, e == null ? void 0 : this.off + e);
  }
  forward(e) {
    e == this.len ? this.next() : (this.len -= e, this.off += e);
  }
  forward2(e) {
    this.ins == -1 ? this.forward(e) : e == this.ins ? this.next() : (this.ins -= e, this.off += e);
  }
}
class F {
  static {
    u(this, "SelectionRange");
  }
  constructor(e, t, n) {
    this.from = e, this.to = t, this.flags = n;
  }
  /**
  The anchor of the range—the side that doesn't move when you
  extend it.
  */
  get anchor() {
    return this.flags & 32 ? this.to : this.from;
  }
  /**
  The head of the range, which is moved when the range is
  [extended](https://codemirror.net/6/docs/ref/#state.SelectionRange.extend).
  */
  get head() {
    return this.flags & 32 ? this.from : this.to;
  }
  /**
  True when `anchor` and `head` are at the same position.
  */
  get empty() {
    return this.from == this.to;
  }
  /**
  If this is a cursor that is explicitly associated with the
  character on one of its sides, this returns the side. -1 means
  the character before its position, 1 the character after, and 0
  means no association.
  */
  get assoc() {
    return this.flags & 8 ? -1 : this.flags & 16 ? 1 : 0;
  }
  /**
  The bidirectional text level associated with this cursor, if
  any.
  */
  get bidiLevel() {
    let e = this.flags & 7;
    return e == 7 ? null : e;
  }
  /**
  The goal column (stored vertical offset) associated with a
  cursor. This is used to preserve the vertical position when
  [moving](https://codemirror.net/6/docs/ref/#view.EditorView.moveVertically) across
  lines of different length.
  */
  get goalColumn() {
    let e = this.flags >> 6;
    return e == 16777215 ? void 0 : e;
  }
  /**
  Map this range through a change, producing a valid range in the
  updated document.
  */
  map(e, t = -1) {
    let n, i;
    return this.empty ? n = i = e.mapPos(this.from, t) : (n = e.mapPos(this.from, 1), i = e.mapPos(this.to, -1)), n == this.from && i == this.to ? this : new F(n, i, this.flags);
  }
  /**
  Extend this range to cover at least `from` to `to`.
  */
  extend(e, t = e) {
    if (e <= this.anchor && t >= this.anchor)
      return p.range(e, t);
    let n = Math.abs(e - this.anchor) > Math.abs(t - this.anchor) ? e : t;
    return p.range(this.anchor, n);
  }
  /**
  Compare this range to another range.
  */
  eq(e, t = !1) {
    return this.anchor == e.anchor && this.head == e.head && (!t || !this.empty || this.assoc == e.assoc);
  }
  /**
  Return a JSON-serializable object representing the range.
  */
  toJSON() {
    return { anchor: this.anchor, head: this.head };
  }
  /**
  Convert a JSON representation of a range to a `SelectionRange`
  instance.
  */
  static fromJSON(e) {
    if (!e || typeof e.anchor != "number" || typeof e.head != "number")
      throw new RangeError("Invalid JSON representation for SelectionRange");
    return p.range(e.anchor, e.head);
  }
  /**
  @internal
  */
  static create(e, t, n) {
    return new F(e, t, n);
  }
}
class p {
  static {
    u(this, "EditorSelection");
  }
  constructor(e, t) {
    this.ranges = e, this.mainIndex = t;
  }
  /**
  Map a selection through a change. Used to adjust the selection
  position for changes.
  */
  map(e, t = -1) {
    return e.empty ? this : p.create(this.ranges.map((n) => n.map(e, t)), this.mainIndex);
  }
  /**
  Compare this selection to another selection. By default, ranges
  are compared only by position. When `includeAssoc` is true,
  cursor ranges must also have the same
  [`assoc`](https://codemirror.net/6/docs/ref/#state.SelectionRange.assoc) value.
  */
  eq(e, t = !1) {
    if (this.ranges.length != e.ranges.length || this.mainIndex != e.mainIndex)
      return !1;
    for (let n = 0; n < this.ranges.length; n++)
      if (!this.ranges[n].eq(e.ranges[n], t))
        return !1;
    return !0;
  }
  /**
  Get the primary selection range. Usually, you should make sure
  your code applies to _all_ ranges, by using methods like
  [`changeByRange`](https://codemirror.net/6/docs/ref/#state.EditorState.changeByRange).
  */
  get main() {
    return this.ranges[this.mainIndex];
  }
  /**
  Make sure the selection only has one range. Returns a selection
  holding only the main range from this selection.
  */
  asSingle() {
    return this.ranges.length == 1 ? this : new p([this.main], 0);
  }
  /**
  Extend this selection with an extra range.
  */
  addRange(e, t = !0) {
    return p.create([e].concat(this.ranges), t ? 0 : this.mainIndex + 1);
  }
  /**
  Replace a given range with another range, and then normalize the
  selection to merge and sort ranges if necessary.
  */
  replaceRange(e, t = this.mainIndex) {
    let n = this.ranges.slice();
    return n[t] = e, p.create(n, this.mainIndex);
  }
  /**
  Convert this selection to an object that can be serialized to
  JSON.
  */
  toJSON() {
    return { ranges: this.ranges.map((e) => e.toJSON()), main: this.mainIndex };
  }
  /**
  Create a selection from a JSON representation.
  */
  static fromJSON(e) {
    if (!e || !Array.isArray(e.ranges) || typeof e.main != "number" || e.main >= e.ranges.length)
      throw new RangeError("Invalid JSON representation for EditorSelection");
    return new p(e.ranges.map((t) => F.fromJSON(t)), e.main);
  }
  /**
  Create a selection holding a single range.
  */
  static single(e, t = e) {
    return new p([p.range(e, t)], 0);
  }
  /**
  Sort and merge the given set of ranges, creating a valid
  selection.
  */
  static create(e, t = 0) {
    if (e.length == 0)
      throw new RangeError("A selection needs at least one range");
    for (let n = 0, i = 0; i < e.length; i++) {
      let s = e[i];
      if (s.empty ? s.from <= n : s.from < n)
        return p.normalized(e.slice(), t);
      n = s.to;
    }
    return new p(e, t);
  }
  /**
  Create a cursor selection range at the given position. You can
  safely ignore the optional arguments in most situations.
  */
  static cursor(e, t = 0, n, i) {
    return F.create(e, e, (t == 0 ? 0 : t < 0 ? 8 : 16) | (n == null ? 7 : Math.min(6, n)) | (i ?? 16777215) << 6);
  }
  /**
  Create a selection range.
  */
  static range(e, t, n, i) {
    let s = (n ?? 16777215) << 6 | (i == null ? 7 : Math.min(6, i));
    return t < e ? F.create(t, e, 48 | s) : F.create(e, t, (t > e ? 8 : 0) | s);
  }
  /**
  @internal
  */
  static normalized(e, t = 0) {
    let n = e[t];
    e.sort((i, s) => i.from - s.from), t = e.indexOf(n);
    for (let i = 1; i < e.length; i++) {
      let s = e[i], r = e[i - 1];
      if (s.empty ? s.from <= r.to : s.from < r.to) {
        let h = r.from, o = Math.max(s.to, r.to);
        i <= t && t--, e.splice(--i, 2, s.anchor > s.head ? p.range(o, h) : p.range(h, o));
      }
    }
    return new p(e, t);
  }
}
function Ee(l, e) {
  for (let t of l.ranges)
    if (t.to > e)
      throw new RangeError("Selection points outside of document");
}
u(Ee, "checkSelection");
let pe = 0;
class E {
  static {
    u(this, "Facet");
  }
  constructor(e, t, n, i, s) {
    this.combine = e, this.compareInput = t, this.compare = n, this.isStatic = i, this.id = pe++, this.default = e([]), this.extensions = typeof s == "function" ? s(this) : s;
  }
  /**
  Returns a facet reader for this facet, which can be used to
  [read](https://codemirror.net/6/docs/ref/#state.EditorState.facet) it but not to define values for it.
  */
  get reader() {
    return this;
  }
  /**
  Define a new facet.
  */
  static define(e = {}) {
    return new E(e.combine || ((t) => t), e.compareInput || ((t, n) => t === n), e.compare || (e.combine ? (t, n) => t === n : me), !!e.static, e.enables);
  }
  /**
  Returns an extension that adds the given value to this facet.
  */
  of(e) {
    return new Y([], this, 0, e);
  }
  /**
  Create an extension that computes a value for the facet from a
  state. You must take care to declare the parts of the state that
  this value depends on, since your function is only called again
  for a new state when one of those parts changed.
  
  In cases where your value depends only on a single field, you'll
  want to use the [`from`](https://codemirror.net/6/docs/ref/#state.Facet.from) method instead.
  */
  compute(e, t) {
    if (this.isStatic)
      throw new Error("Can't compute a static facet");
    return new Y(e, this, 1, t);
  }
  /**
  Create an extension that computes zero or more values for this
  facet from a state.
  */
  computeN(e, t) {
    if (this.isStatic)
      throw new Error("Can't compute a static facet");
    return new Y(e, this, 2, t);
  }
  from(e, t) {
    return t || (t = /* @__PURE__ */ u((n) => n, "get")), this.compute([e], (n) => t(n.field(e)));
  }
}
function me(l, e) {
  return l == e || l.length == e.length && l.every((t, n) => t === e[n]);
}
u(me, "sameArray");
class Y {
  static {
    u(this, "FacetProvider");
  }
  constructor(e, t, n, i) {
    this.dependencies = e, this.facet = t, this.type = n, this.value = i, this.id = pe++;
  }
  dynamicSlot(e) {
    var t;
    let n = this.value, i = this.facet.compareInput, s = this.id, r = e[s] >> 1, h = this.type == 2, o = !1, a = !1, f = [];
    for (let c of this.dependencies)
      c == "doc" ? o = !0 : c == "selection" ? a = !0 : ((t = e[c.id]) !== null && t !== void 0 ? t : 1) & 1 || f.push(e[c.id]);
    return {
      create(c) {
        return c.values[r] = n(c), 1;
      },
      update(c, d) {
        if (o && d.docChanged || a && (d.docChanged || d.selection) || oe(c, f)) {
          let g = n(c);
          if (h ? !xe(g, c.values[r], i) : !i(g, c.values[r]))
            return c.values[r] = g, 1;
        }
        return 0;
      },
      reconfigure: /* @__PURE__ */ u((c, d) => {
        let g, m = d.config.address[s];
        if (m != null) {
          let O = _(d, m);
          if (this.dependencies.every((x) => x instanceof E ? d.facet(x) === c.facet(x) : x instanceof V ? d.field(x, !1) == c.field(x, !1) : !0) || (h ? xe(g = n(c), O, i) : i(g = n(c), O)))
            return c.values[r] = O, 0;
        } else
          g = n(c);
        return c.values[r] = g, 1;
      }, "reconfigure")
    };
  }
}
function xe(l, e, t) {
  if (l.length != e.length)
    return !1;
  for (let n = 0; n < l.length; n++)
    if (!t(l[n], e[n]))
      return !1;
  return !0;
}
u(xe, "compareArray");
function oe(l, e) {
  let t = !1;
  for (let n of e)
    q(l, n) & 1 && (t = !0);
  return t;
}
u(oe, "ensureAll");
function We(l, e, t) {
  let n = t.map((o) => l[o.id]), i = t.map((o) => o.type), s = n.filter((o) => !(o & 1)), r = l[e.id] >> 1;
  function h(o) {
    let a = [];
    for (let f = 0; f < n.length; f++) {
      let c = _(o, n[f]);
      if (i[f] == 2)
        for (let d of c)
          a.push(d);
      else
        a.push(c);
    }
    return e.combine(a);
  }
  return u(h, "get"), {
    create(o) {
      for (let a of n)
        q(o, a);
      return o.values[r] = h(o), 1;
    },
    update(o, a) {
      if (!oe(o, s))
        return 0;
      let f = h(o);
      return e.compare(f, o.values[r]) ? 0 : (o.values[r] = f, 1);
    },
    reconfigure(o, a) {
      let f = oe(o, n), c = a.config.facets[e.id], d = a.facet(e);
      if (c && !f && me(t, c))
        return o.values[r] = d, 0;
      let g = h(o);
      return e.compare(g, d) ? (o.values[r] = d, 0) : (o.values[r] = g, 1);
    }
  };
}
u(We, "dynamicFacetSlot");
const H = /* @__PURE__ */ E.define({ static: !0 });
class V {
  static {
    u(this, "StateField");
  }
  constructor(e, t, n, i, s) {
    this.id = e, this.createF = t, this.updateF = n, this.compareF = i, this.spec = s, this.provides = void 0;
  }
  /**
  Define a state field.
  */
  static define(e) {
    let t = new V(pe++, e.create, e.update, e.compare || ((n, i) => n === i), e);
    return e.provide && (t.provides = e.provide(t)), t;
  }
  create(e) {
    let t = e.facet(H).find((n) => n.field == this);
    return (t?.create || this.createF)(e);
  }
  /**
  @internal
  */
  slot(e) {
    let t = e[this.id] >> 1;
    return {
      create: /* @__PURE__ */ u((n) => (n.values[t] = this.create(n), 1), "create"),
      update: /* @__PURE__ */ u((n, i) => {
        let s = n.values[t], r = this.updateF(s, i);
        return this.compareF(s, r) ? 0 : (n.values[t] = r, 1);
      }, "update"),
      reconfigure: /* @__PURE__ */ u((n, i) => {
        let s = n.facet(H), r = i.facet(H), h;
        return (h = s.find((o) => o.field == this)) && h != r.find((o) => o.field == this) ? (n.values[t] = h.create(n), 1) : i.config.address[this.id] != null ? (n.values[t] = i.field(this), 0) : (n.values[t] = this.create(n), 1);
      }, "reconfigure")
    };
  }
  /**
  Returns an extension that enables this field and overrides the
  way it is initialized. Can be useful when you need to provide a
  non-default starting value for the field.
  */
  init(e) {
    return [this, H.of({ field: this, create: e })];
  }
  /**
  State field instances can be used as
  [`Extension`](https://codemirror.net/6/docs/ref/#state.Extension) values to enable the field in a
  given state.
  */
  get extension() {
    return this;
  }
}
const B = { lowest: 4, low: 3, default: 2, high: 1, highest: 0 };
function N(l) {
  return (e) => new Oe(e, l);
}
u(N, "prec");
const lt = {
  /**
  The highest precedence level, for extensions that should end up
  near the start of the precedence ordering.
  */
  highest: /* @__PURE__ */ N(B.highest),
  /**
  A higher-than-default precedence, for extensions that should
  come before those with default precedence.
  */
  high: /* @__PURE__ */ N(B.high),
  /**
  The default precedence, which is also used for extensions
  without an explicit precedence.
  */
  default: /* @__PURE__ */ N(B.default),
  /**
  A lower-than-default precedence.
  */
  low: /* @__PURE__ */ N(B.low),
  /**
  The lowest precedence level. Meant for things that should end up
  near the end of the extension order.
  */
  lowest: /* @__PURE__ */ N(B.lowest)
};
class Oe {
  static {
    u(this, "PrecExtension");
  }
  constructor(e, t) {
    this.inner = e, this.prec = t;
  }
}
class ne {
  static {
    u(this, "Compartment");
  }
  /**
  Create an instance of this compartment to add to your [state
  configuration](https://codemirror.net/6/docs/ref/#state.EditorStateConfig.extensions).
  */
  of(e) {
    return new ae(this, e);
  }
  /**
  Create an [effect](https://codemirror.net/6/docs/ref/#state.TransactionSpec.effects) that
  reconfigures this compartment.
  */
  reconfigure(e) {
    return ne.reconfigure.of({ compartment: this, extension: e });
  }
  /**
  Get the current content of the compartment in the state, or
  `undefined` if it isn't present.
  */
  get(e) {
    return e.config.compartments.get(this);
  }
}
class ae {
  static {
    u(this, "CompartmentInstance");
  }
  constructor(e, t) {
    this.compartment = e, this.inner = t;
  }
}
class j {
  static {
    u(this, "Configuration");
  }
  constructor(e, t, n, i, s, r) {
    for (this.base = e, this.compartments = t, this.dynamicSlots = n, this.address = i, this.staticValues = s, this.facets = r, this.statusTemplate = []; this.statusTemplate.length < n.length; )
      this.statusTemplate.push(
        0
        /* SlotStatus.Unresolved */
      );
  }
  staticFacet(e) {
    let t = this.address[e.id];
    return t == null ? e.default : this.staticValues[t >> 1];
  }
  static resolve(e, t, n) {
    let i = [], s = /* @__PURE__ */ Object.create(null), r = /* @__PURE__ */ new Map();
    for (let d of Ue(e, t, r))
      d instanceof V ? i.push(d) : (s[d.facet.id] || (s[d.facet.id] = [])).push(d);
    let h = /* @__PURE__ */ Object.create(null), o = [], a = [];
    for (let d of i)
      h[d.id] = a.length << 1, a.push((g) => d.slot(g));
    let f = n?.config.facets;
    for (let d in s) {
      let g = s[d], m = g[0].facet, O = f && f[d] || [];
      if (g.every(
        (x) => x.type == 0
        /* Provider.Static */
      ))
        if (h[m.id] = o.length << 1 | 1, me(O, g))
          o.push(n.facet(m));
        else {
          let x = m.combine(g.map((ie) => ie.value));
          o.push(n && m.compare(x, n.facet(m)) ? n.facet(m) : x);
        }
      else {
        for (let x of g)
          x.type == 0 ? (h[x.id] = o.length << 1 | 1, o.push(x.value)) : (h[x.id] = a.length << 1, a.push((ie) => x.dynamicSlot(ie)));
        h[m.id] = a.length << 1, a.push((x) => We(x, m, g));
      }
    }
    let c = a.map((d) => d(h));
    return new j(e, r, c, h, o, s);
  }
}
function Ue(l, e, t) {
  let n = [[], [], [], [], []], i = /* @__PURE__ */ new Map();
  function s(r, h) {
    let o = i.get(r);
    if (o != null) {
      if (o <= h)
        return;
      let a = n[o].indexOf(r);
      a > -1 && n[o].splice(a, 1), r instanceof ae && t.delete(r.compartment);
    }
    if (i.set(r, h), Array.isArray(r))
      for (let a of r)
        s(a, h);
    else if (r instanceof ae) {
      if (t.has(r.compartment))
        throw new RangeError("Duplicate use of compartment in extensions");
      let a = e.get(r.compartment) || r.inner;
      t.set(r.compartment, a), s(a, h);
    } else if (r instanceof Oe)
      s(r.inner, r.prec);
    else if (r instanceof V)
      n[h].push(r), r.provides && s(r.provides, h);
    else if (r instanceof Y)
      n[h].push(r), r.facet.extensions && s(r.facet.extensions, B.default);
    else {
      let a = r.extension;
      if (!a)
        throw new Error(`Unrecognized extension value in extension set (${r}). This sometimes happens because multiple instances of @codemirror/state are loaded, breaking instanceof checks.`);
      s(a, h);
    }
  }
  return u(s, "inner"), s(l, B.default), n.reduce((r, h) => r.concat(h));
}
u(Ue, "flatten");
function q(l, e) {
  if (e & 1)
    return 2;
  let t = e >> 1, n = l.status[t];
  if (n == 4)
    throw new Error("Cyclic dependency between fields and/or facets");
  if (n & 2)
    return n;
  l.status[t] = 4;
  let i = l.computeSlot(l, l.config.dynamicSlots[t]);
  return l.status[t] = 2 | i;
}
u(q, "ensureAddr");
function _(l, e) {
  return e & 1 ? l.config.staticValues[e >> 1] : l.values[e >> 1];
}
u(_, "getAddr");
const Ce = /* @__PURE__ */ E.define(), fe = /* @__PURE__ */ E.define({
  combine: /* @__PURE__ */ u((l) => l.some((e) => e), "combine"),
  static: !0
}), Me = /* @__PURE__ */ E.define({
  combine: /* @__PURE__ */ u((l) => l.length ? l[0] : void 0, "combine"),
  static: !0
}), be = /* @__PURE__ */ E.define(), Re = /* @__PURE__ */ E.define(), Te = /* @__PURE__ */ E.define(), Be = /* @__PURE__ */ E.define({
  combine: /* @__PURE__ */ u((l) => l.length ? l[0] : !1, "combine")
});
class G {
  static {
    u(this, "Annotation");
  }
  /**
  @internal
  */
  constructor(e, t) {
    this.type = e, this.value = t;
  }
  /**
  Define a new type of annotation.
  */
  static define() {
    return new Ge();
  }
}
class Ge {
  static {
    u(this, "AnnotationType");
  }
  /**
  Create an instance of this annotation.
  */
  of(e) {
    return new G(this, e);
  }
}
class He {
  static {
    u(this, "StateEffectType");
  }
  /**
  @internal
  */
  constructor(e) {
    this.map = e;
  }
  /**
  Create a [state effect](https://codemirror.net/6/docs/ref/#state.StateEffect) instance of this
  type.
  */
  of(e) {
    return new P(this, e);
  }
}
class P {
  static {
    u(this, "StateEffect");
  }
  /**
  @internal
  */
  constructor(e, t) {
    this.type = e, this.value = t;
  }
  /**
  Map this effect through a position mapping. Will return
  `undefined` when that ends up deleting the effect.
  */
  map(e) {
    let t = this.type.map(this.value, e);
    return t === void 0 ? void 0 : t == this.value ? this : new P(this.type, t);
  }
  /**
  Tells you whether this effect object is of a given
  [type](https://codemirror.net/6/docs/ref/#state.StateEffectType).
  */
  is(e) {
    return this.type == e;
  }
  /**
  Define a new effect type. The type parameter indicates the type
  of values that his effect holds. It should be a type that
  doesn't include `undefined`, since that is used in
  [mapping](https://codemirror.net/6/docs/ref/#state.StateEffect.map) to indicate that an effect is
  removed.
  */
  static define(e = {}) {
    return new He(e.map || ((t) => t));
  }
  /**
  Map an array of effects through a change set.
  */
  static mapEffects(e, t) {
    if (!e.length)
      return e;
    let n = [];
    for (let i of e) {
      let s = i.map(t);
      s && n.push(s);
    }
    return n;
  }
}
P.reconfigure = /* @__PURE__ */ P.define();
P.appendConfig = /* @__PURE__ */ P.define();
class A {
  static {
    u(this, "Transaction");
  }
  constructor(e, t, n, i, s, r) {
    this.startState = e, this.changes = t, this.selection = n, this.effects = i, this.annotations = s, this.scrollIntoView = r, this._doc = null, this._state = null, n && Ee(n, t.newLength), s.some((h) => h.type == A.time) || (this.annotations = s.concat(A.time.of(Date.now())));
  }
  /**
  @internal
  */
  static create(e, t, n, i, s, r) {
    return new A(e, t, n, i, s, r);
  }
  /**
  The new document produced by the transaction. Contrary to
  [`.state`](https://codemirror.net/6/docs/ref/#state.Transaction.state)`.doc`, accessing this won't
  force the entire new state to be computed right away, so it is
  recommended that [transaction
  filters](https://codemirror.net/6/docs/ref/#state.EditorState^transactionFilter) use this getter
  when they need to look at the new document.
  */
  get newDoc() {
    return this._doc || (this._doc = this.changes.apply(this.startState.doc));
  }
  /**
  The new selection produced by the transaction. If
  [`this.selection`](https://codemirror.net/6/docs/ref/#state.Transaction.selection) is undefined,
  this will [map](https://codemirror.net/6/docs/ref/#state.EditorSelection.map) the start state's
  current selection through the changes made by the transaction.
  */
  get newSelection() {
    return this.selection || this.startState.selection.map(this.changes);
  }
  /**
  The new state created by the transaction. Computed on demand
  (but retained for subsequent access), so it is recommended not to
  access it in [transaction
  filters](https://codemirror.net/6/docs/ref/#state.EditorState^transactionFilter) when possible.
  */
  get state() {
    return this._state || this.startState.applyTransaction(this), this._state;
  }
  /**
  Get the value of the given annotation type, if any.
  */
  annotation(e) {
    for (let t of this.annotations)
      if (t.type == e)
        return t.value;
  }
  /**
  Indicates whether the transaction changed the document.
  */
  get docChanged() {
    return !this.changes.empty;
  }
  /**
  Indicates whether this transaction reconfigures the state
  (through a [configuration compartment](https://codemirror.net/6/docs/ref/#state.Compartment) or
  with a top-level configuration
  [effect](https://codemirror.net/6/docs/ref/#state.StateEffect^reconfigure).
  */
  get reconfigured() {
    return this.startState.config != this.state.config;
  }
  /**
  Returns true if the transaction has a [user
  event](https://codemirror.net/6/docs/ref/#state.Transaction^userEvent) annotation that is equal to
  or more specific than `event`. For example, if the transaction
  has `"select.pointer"` as user event, `"select"` and
  `"select.pointer"` will match it.
  */
  isUserEvent(e) {
    let t = this.annotation(A.userEvent);
    return !!(t && (t == e || t.length > e.length && t.slice(0, e.length) == e && t[e.length] == "."));
  }
}
A.time = /* @__PURE__ */ G.define();
A.userEvent = /* @__PURE__ */ G.define();
A.addToHistory = /* @__PURE__ */ G.define();
A.remote = /* @__PURE__ */ G.define();
function Ke(l, e) {
  let t = [];
  for (let n = 0, i = 0; ; ) {
    let s, r;
    if (n < l.length && (i == e.length || e[i] >= l[n]))
      s = l[n++], r = l[n++];
    else if (i < e.length)
      s = e[i++], r = e[i++];
    else
      return t;
    !t.length || t[t.length - 1] < s ? t.push(s, r) : t[t.length - 1] < r && (t[t.length - 1] = r);
  }
}
u(Ke, "joinRanges");
function Fe(l, e, t) {
  var n;
  let i, s, r;
  return t ? (i = e.changes, s = k.empty(e.changes.length), r = l.changes.compose(e.changes)) : (i = e.changes.map(l.changes), s = l.changes.mapDesc(e.changes, !0), r = l.changes.compose(i)), {
    changes: r,
    selection: e.selection ? e.selection.map(s) : (n = l.selection) === null || n === void 0 ? void 0 : n.map(i),
    effects: P.mapEffects(l.effects, i).concat(P.mapEffects(e.effects, s)),
    annotations: l.annotations.length ? l.annotations.concat(e.annotations) : e.annotations,
    scrollIntoView: l.scrollIntoView || e.scrollIntoView
  };
}
u(Fe, "mergeTransaction");
function ue(l, e, t) {
  let n = e.selection, i = J(e.annotations);
  return e.userEvent && (i = i.concat(A.userEvent.of(e.userEvent))), {
    changes: e.changes instanceof k ? e.changes : k.of(e.changes || [], t, l.facet(Me)),
    selection: n && (n instanceof p ? n : p.single(n.anchor, n.head)),
    effects: J(e.effects),
    annotations: i,
    scrollIntoView: !!e.scrollIntoView
  };
}
u(ue, "resolveTransactionInner");
function Je(l, e, t) {
  let n = ue(l, e.length ? e[0] : {}, l.doc.length);
  e.length && e[0].filter === !1 && (t = !1);
  for (let s = 1; s < e.length; s++) {
    e[s].filter === !1 && (t = !1);
    let r = !!e[s].sequential;
    n = Fe(n, ue(l, e[s], r ? n.changes.newLength : l.doc.length), r);
  }
  let i = A.create(l, n.changes, n.selection, n.effects, n.annotations, n.scrollIntoView);
  return Xe(t ? Qe(i) : i);
}
u(Je, "resolveTransaction");
function Qe(l) {
  let e = l.startState, t = !0;
  for (let i of e.facet(be)) {
    let s = i(l);
    if (s === !1) {
      t = !1;
      break;
    }
    Array.isArray(s) && (t = t === !0 ? s : Ke(t, s));
  }
  if (t !== !0) {
    let i, s;
    if (t === !1)
      s = l.changes.invertedDesc, i = k.empty(e.doc.length);
    else {
      let r = l.changes.filter(t);
      i = r.changes, s = r.filtered.mapDesc(r.changes).invertedDesc;
    }
    l = A.create(e, i, l.selection && l.selection.map(s), P.mapEffects(l.effects, s), l.annotations, l.scrollIntoView);
  }
  let n = e.facet(Re);
  for (let i = n.length - 1; i >= 0; i--) {
    let s = n[i](l);
    s instanceof A ? l = s : Array.isArray(s) && s.length == 1 && s[0] instanceof A ? l = s[0] : l = Je(e, J(s), !1);
  }
  return l;
}
u(Qe, "filterTransaction");
function Xe(l) {
  let e = l.startState, t = e.facet(Te), n = l;
  for (let i = t.length - 1; i >= 0; i--) {
    let s = t[i](l);
    s && Object.keys(s).length && (n = Fe(n, ue(e, s, l.changes.newLength), !0));
  }
  return n == l ? l : A.create(e, l.changes, l.selection, n.effects, n.annotations, n.scrollIntoView);
}
u(Xe, "extendTransaction");
const Ye = [];
function J(l) {
  return l == null ? Ye : Array.isArray(l) ? l : [l];
}
u(J, "asArray");
var T = /* @__PURE__ */ function(l) {
  return l[l.Word = 0] = "Word", l[l.Space = 1] = "Space", l[l.Other = 2] = "Other", l;
}(T || (T = {}));
const Ze = /[\u00df\u0587\u0590-\u05f4\u0600-\u06ff\u3040-\u309f\u30a0-\u30ff\u3400-\u4db5\u4e00-\u9fcc\uac00-\ud7af]/;
let ce;
try {
  ce = /* @__PURE__ */ new RegExp("[\\p{Alphabetic}\\p{Number}_]", "u");
} catch {
}
function je(l) {
  if (ce)
    return ce.test(l);
  for (let e = 0; e < l.length; e++) {
    let t = l[e];
    if (/\w/.test(t) || t > "" && (t.toUpperCase() != t.toLowerCase() || Ze.test(t)))
      return !0;
  }
  return !1;
}
u(je, "hasWordChar");
function _e(l) {
  return (e) => {
    if (!/\S/.test(e))
      return T.Space;
    if (je(e))
      return T.Word;
    for (let t = 0; t < l.length; t++)
      if (e.indexOf(l[t]) > -1)
        return T.Word;
    return T.Other;
  };
}
u(_e, "makeCategorizer");
class v {
  static {
    u(this, "EditorState");
  }
  constructor(e, t, n, i, s, r) {
    this.config = e, this.doc = t, this.selection = n, this.values = i, this.status = e.statusTemplate.slice(), this.computeSlot = s, r && (r._state = this);
    for (let h = 0; h < this.config.dynamicSlots.length; h++)
      q(this, h << 1);
    this.computeSlot = null;
  }
  field(e, t = !0) {
    let n = this.config.address[e.id];
    if (n == null) {
      if (t)
        throw new RangeError("Field is not present in this state");
      return;
    }
    return q(this, n), _(this, n);
  }
  /**
  Create a [transaction](https://codemirror.net/6/docs/ref/#state.Transaction) that updates this
  state. Any number of [transaction specs](https://codemirror.net/6/docs/ref/#state.TransactionSpec)
  can be passed. Unless
  [`sequential`](https://codemirror.net/6/docs/ref/#state.TransactionSpec.sequential) is set, the
  [changes](https://codemirror.net/6/docs/ref/#state.TransactionSpec.changes) (if any) of each spec
  are assumed to start in the _current_ document (not the document
  produced by previous specs), and its
  [selection](https://codemirror.net/6/docs/ref/#state.TransactionSpec.selection) and
  [effects](https://codemirror.net/6/docs/ref/#state.TransactionSpec.effects) are assumed to refer
  to the document created by its _own_ changes. The resulting
  transaction contains the combined effect of all the different
  specs. For [selection](https://codemirror.net/6/docs/ref/#state.TransactionSpec.selection), later
  specs take precedence over earlier ones.
  */
  update(...e) {
    return Je(this, e, !0);
  }
  /**
  @internal
  */
  applyTransaction(e) {
    let t = this.config, { base: n, compartments: i } = t;
    for (let h of e.effects)
      h.is(ne.reconfigure) ? (t && (i = /* @__PURE__ */ new Map(), t.compartments.forEach((o, a) => i.set(a, o)), t = null), i.set(h.value.compartment, h.value.extension)) : h.is(P.reconfigure) ? (t = null, n = h.value) : h.is(P.appendConfig) && (t = null, n = J(n).concat(h.value));
    let s;
    t ? s = e.startState.values.slice() : (t = j.resolve(n, i, this), s = new v(t, this.doc, this.selection, t.dynamicSlots.map(() => null), (o, a) => a.reconfigure(o, this), null).values);
    let r = e.startState.facet(fe) ? e.newSelection : e.newSelection.asSingle();
    new v(t, e.newDoc, r, s, (h, o) => o.update(h, e), e);
  }
  /**
  Create a [transaction spec](https://codemirror.net/6/docs/ref/#state.TransactionSpec) that
  replaces every selection range with the given content.
  */
  replaceSelection(e) {
    return typeof e == "string" && (e = this.toText(e)), this.changeByRange((t) => ({
      changes: { from: t.from, to: t.to, insert: e },
      range: p.cursor(t.from + e.length)
    }));
  }
  /**
  Create a set of changes and a new selection by running the given
  function for each range in the active selection. The function
  can return an optional set of changes (in the coordinate space
  of the start document), plus an updated range (in the coordinate
  space of the document produced by the call's own changes). This
  method will merge all the changes and ranges into a single
  changeset and selection, and return it as a [transaction
  spec](https://codemirror.net/6/docs/ref/#state.TransactionSpec), which can be passed to
  [`update`](https://codemirror.net/6/docs/ref/#state.EditorState.update).
  */
  changeByRange(e) {
    let t = this.selection, n = e(t.ranges[0]), i = this.changes(n.changes), s = [n.range], r = J(n.effects);
    for (let h = 1; h < t.ranges.length; h++) {
      let o = e(t.ranges[h]), a = this.changes(o.changes), f = a.map(i);
      for (let d = 0; d < h; d++)
        s[d] = s[d].map(f);
      let c = i.mapDesc(a, !0);
      s.push(o.range.map(c)), i = i.compose(f), r = P.mapEffects(r, f).concat(P.mapEffects(J(o.effects), c));
    }
    return {
      changes: i,
      selection: p.create(s, t.mainIndex),
      effects: r
    };
  }
  /**
  Create a [change set](https://codemirror.net/6/docs/ref/#state.ChangeSet) from the given change
  description, taking the state's document length and line
  separator into account.
  */
  changes(e = []) {
    return e instanceof k ? e : k.of(e, this.doc.length, this.facet(v.lineSeparator));
  }
  /**
  Using the state's [line
  separator](https://codemirror.net/6/docs/ref/#state.EditorState^lineSeparator), create a
  [`Text`](https://codemirror.net/6/docs/ref/#state.Text) instance from the given string.
  */
  toText(e) {
    return y.of(e.split(this.facet(v.lineSeparator) || re));
  }
  /**
  Return the given range of the document as a string.
  */
  sliceDoc(e = 0, t = this.doc.length) {
    return this.doc.sliceString(e, t, this.lineBreak);
  }
  /**
  Get the value of a state [facet](https://codemirror.net/6/docs/ref/#state.Facet).
  */
  facet(e) {
    let t = this.config.address[e.id];
    return t == null ? e.default : (q(this, t), _(this, t));
  }
  /**
  Convert this state to a JSON-serializable object. When custom
  fields should be serialized, you can pass them in as an object
  mapping property names (in the resulting object, which should
  not use `doc` or `selection`) to fields.
  */
  toJSON(e) {
    let t = {
      doc: this.sliceDoc(),
      selection: this.selection.toJSON()
    };
    if (e)
      for (let n in e) {
        let i = e[n];
        i instanceof V && this.config.address[i.id] != null && (t[n] = i.spec.toJSON(this.field(e[n]), this));
      }
    return t;
  }
  /**
  Deserialize a state from its JSON representation. When custom
  fields should be deserialized, pass the same object you passed
  to [`toJSON`](https://codemirror.net/6/docs/ref/#state.EditorState.toJSON) when serializing as
  third argument.
  */
  static fromJSON(e, t = {}, n) {
    if (!e || typeof e.doc != "string")
      throw new RangeError("Invalid JSON representation for EditorState");
    let i = [];
    if (n) {
      for (let s in n)
        if (Object.prototype.hasOwnProperty.call(e, s)) {
          let r = n[s], h = e[s];
          i.push(r.init((o) => r.spec.fromJSON(h, o)));
        }
    }
    return v.create({
      doc: e.doc,
      selection: p.fromJSON(e.selection),
      extensions: t.extensions ? i.concat([t.extensions]) : i
    });
  }
  /**
  Create a new state. You'll usually only need this when
  initializing an editor—updated states are created by applying
  transactions.
  */
  static create(e = {}) {
    let t = j.resolve(e.extensions || [], /* @__PURE__ */ new Map()), n = e.doc instanceof y ? e.doc : y.of((e.doc || "").split(t.staticFacet(v.lineSeparator) || re)), i = e.selection ? e.selection instanceof p ? e.selection : p.single(e.selection.anchor, e.selection.head) : p.single(0);
    return Ee(i, n.length), t.staticFacet(fe) || (i = i.asSingle()), new v(t, n, i, t.dynamicSlots.map(() => null), (s, r) => r.create(s), null);
  }
  /**
  The size (in columns) of a tab in the document, determined by
  the [`tabSize`](https://codemirror.net/6/docs/ref/#state.EditorState^tabSize) facet.
  */
  get tabSize() {
    return this.facet(v.tabSize);
  }
  /**
  Get the proper [line-break](https://codemirror.net/6/docs/ref/#state.EditorState^lineSeparator)
  string for this state.
  */
  get lineBreak() {
    return this.facet(v.lineSeparator) || `
`;
  }
  /**
  Returns true when the editor is
  [configured](https://codemirror.net/6/docs/ref/#state.EditorState^readOnly) to be read-only.
  */
  get readOnly() {
    return this.facet(Be);
  }
  /**
  Look up a translation for the given phrase (via the
  [`phrases`](https://codemirror.net/6/docs/ref/#state.EditorState^phrases) facet), or return the
  original string if no translation is found.
  
  If additional arguments are passed, they will be inserted in
  place of markers like `$1` (for the first value) and `$2`, etc.
  A single `$` is equivalent to `$1`, and `$$` will produce a
  literal dollar sign.
  */
  phrase(e, ...t) {
    for (let n of this.facet(v.phrases))
      if (Object.prototype.hasOwnProperty.call(n, e)) {
        e = n[e];
        break;
      }
    return t.length && (e = e.replace(/\$(\$|\d*)/g, (n, i) => {
      if (i == "$")
        return "$";
      let s = +(i || 1);
      return !s || s > t.length ? n : t[s - 1];
    })), e;
  }
  /**
  Find the values for a given language data field, provided by the
  the [`languageData`](https://codemirror.net/6/docs/ref/#state.EditorState^languageData) facet.
  
  Examples of language data fields are...
  
  - [`"commentTokens"`](https://codemirror.net/6/docs/ref/#commands.CommentTokens) for specifying
    comment syntax.
  - [`"autocomplete"`](https://codemirror.net/6/docs/ref/#autocomplete.autocompletion^config.override)
    for providing language-specific completion sources.
  - [`"wordChars"`](https://codemirror.net/6/docs/ref/#state.EditorState.charCategorizer) for adding
    characters that should be considered part of words in this
    language.
  - [`"closeBrackets"`](https://codemirror.net/6/docs/ref/#autocomplete.CloseBracketConfig) controls
    bracket closing behavior.
  */
  languageDataAt(e, t, n = -1) {
    let i = [];
    for (let s of this.facet(Ce))
      for (let r of s(this, t, n))
        Object.prototype.hasOwnProperty.call(r, e) && i.push(r[e]);
    return i;
  }
  /**
  Return a function that can categorize strings (expected to
  represent a single [grapheme cluster](https://codemirror.net/6/docs/ref/#state.findClusterBreak))
  into one of:
  
   - Word (contains an alphanumeric character or a character
     explicitly listed in the local language's `"wordChars"`
     language data, which should be a string)
   - Space (contains only whitespace)
   - Other (anything else)
  */
  charCategorizer(e) {
    return _e(this.languageDataAt("wordChars", e).join(""));
  }
  /**
  Find the word at the given position, meaning the range
  containing all [word](https://codemirror.net/6/docs/ref/#state.CharCategory.Word) characters
  around it. If no word characters are adjacent to the position,
  this returns null.
  */
  wordAt(e) {
    let { text: t, from: n, length: i } = this.doc.lineAt(e), s = this.charCategorizer(e), r = e - n, h = e - n;
    for (; r > 0; ) {
      let o = Z(t, r, !1);
      if (s(t.slice(o, r)) != T.Word)
        break;
      r = o;
    }
    for (; h < i; ) {
      let o = Z(t, h);
      if (s(t.slice(h, o)) != T.Word)
        break;
      h = o;
    }
    return r == h ? null : p.range(r + n, h + n);
  }
}
v.allowMultipleSelections = fe;
v.tabSize = /* @__PURE__ */ E.define({
  combine: /* @__PURE__ */ u((l) => l.length ? l[0] : 4, "combine")
});
v.lineSeparator = Me;
v.readOnly = Be;
v.phrases = /* @__PURE__ */ E.define({
  compare(l, e) {
    let t = Object.keys(l), n = Object.keys(e);
    return t.length == n.length && t.every((i) => l[i] == e[i]);
  }
});
v.languageData = Ce;
v.changeFilter = be;
v.transactionFilter = Re;
v.transactionExtender = Te;
ne.reconfigure = /* @__PURE__ */ P.define();
function ht(l, e, t = {}) {
  let n = {};
  for (let i of l)
    for (let s of Object.keys(i)) {
      let r = i[s], h = n[s];
      if (h === void 0)
        n[s] = r;
      else if (!(h === r || r === void 0)) if (Object.hasOwnProperty.call(t, s))
        n[s] = t[s](h, r);
      else
        throw new Error("Config merge conflict for field " + s);
    }
  for (let i in e)
    n[i] === void 0 && (n[i] = e[i]);
  return n;
}
u(ht, "combineConfig");
class ee {
  static {
    u(this, "RangeValue");
  }
  /**
  Compare this value with another value. Used when comparing
  rangesets. The default implementation compares by identity.
  Unless you are only creating a fixed number of unique instances
  of your value type, it is a good idea to implement this
  properly.
  */
  eq(e) {
    return this == e;
  }
  /**
  Create a [range](https://codemirror.net/6/docs/ref/#state.Range) with this value.
  */
  range(e, t = e) {
    return W.create(e, t, this);
  }
}
ee.prototype.startSide = ee.prototype.endSide = 0;
ee.prototype.point = !1;
ee.prototype.mapMode = b.TrackDel;
class W {
  static {
    u(this, "Range");
  }
  constructor(e, t, n) {
    this.from = e, this.to = t, this.value = n;
  }
  /**
  @internal
  */
  static create(e, t, n) {
    return new W(e, t, n);
  }
}
function de(l, e) {
  return l.from - e.from || l.value.startSide - e.value.startSide;
}
u(de, "cmpRange");
class we {
  static {
    u(this, "Chunk");
  }
  constructor(e, t, n, i) {
    this.from = e, this.to = t, this.value = n, this.maxPoint = i;
  }
  get length() {
    return this.to[this.to.length - 1];
  }
  // Find the index of the given position and side. Use the ranges'
  // `from` pos when `end == false`, `to` when `end == true`.
  findIndex(e, t, n, i = 0) {
    let s = n ? this.to : this.from;
    for (let r = i, h = s.length; ; ) {
      if (r == h)
        return r;
      let o = r + h >> 1, a = s[o] - e || (n ? this.value[o].endSide : this.value[o].startSide) - t;
      if (o == r)
        return a >= 0 ? r : h;
      a >= 0 ? h = o : r = o + 1;
    }
  }
  between(e, t, n, i) {
    for (let s = this.findIndex(t, -1e9, !0), r = this.findIndex(n, 1e9, !1, s); s < r; s++)
      if (i(this.from[s] + e, this.to[s] + e, this.value[s]) === !1)
        return !1;
  }
  map(e, t) {
    let n = [], i = [], s = [], r = -1, h = -1;
    for (let o = 0; o < this.value.length; o++) {
      let a = this.value[o], f = this.from[o] + e, c = this.to[o] + e, d, g;
      if (f == c) {
        let m = t.mapPos(f, a.startSide, a.mapMode);
        if (m == null || (d = g = m, a.startSide != a.endSide && (g = t.mapPos(f, a.endSide), g < d)))
          continue;
      } else if (d = t.mapPos(f, a.startSide), g = t.mapPos(c, a.endSide), d > g || d == g && a.startSide > 0 && a.endSide <= 0)
        continue;
      (g - d || a.endSide - a.startSide) < 0 || (r < 0 && (r = d), a.point && (h = Math.max(h, g - d)), n.push(a), i.push(d - r), s.push(g - r));
    }
    return { mapped: n.length ? new we(i, s, n, h) : null, pos: r };
  }
}
class I {
  static {
    u(this, "RangeSet");
  }
  constructor(e, t, n, i) {
    this.chunkPos = e, this.chunk = t, this.nextLayer = n, this.maxPoint = i;
  }
  /**
  @internal
  */
  static create(e, t, n, i) {
    return new I(e, t, n, i);
  }
  /**
  @internal
  */
  get length() {
    let e = this.chunk.length - 1;
    return e < 0 ? 0 : Math.max(this.chunkEnd(e), this.nextLayer.length);
  }
  /**
  The number of ranges in the set.
  */
  get size() {
    if (this.isEmpty)
      return 0;
    let e = this.nextLayer.size;
    for (let t of this.chunk)
      e += t.value.length;
    return e;
  }
  /**
  @internal
  */
  chunkEnd(e) {
    return this.chunkPos[e] + this.chunk[e].length;
  }
  /**
  Update the range set, optionally adding new ranges or filtering
  out existing ones.
  
  (Note: The type parameter is just there as a kludge to work
  around TypeScript variance issues that prevented `RangeSet<X>`
  from being a subtype of `RangeSet<Y>` when `X` is a subtype of
  `Y`.)
  */
  update(e) {
    let { add: t = [], sort: n = !1, filterFrom: i = 0, filterTo: s = this.length } = e, r = e.filter;
    if (t.length == 0 && !r)
      return this;
    if (n && (t = t.slice().sort(de)), this.isEmpty)
      return t.length ? I.of(t) : this;
    let h = new Le(this, null, -1).goto(0), o = 0, a = [], f = new te();
    for (; h.value || o < t.length; )
      if (o < t.length && (h.from - t[o].from || h.startSide - t[o].value.startSide) >= 0) {
        let c = t[o++];
        f.addInner(c.from, c.to, c.value) || a.push(c);
      } else h.rangeIndex == 1 && h.chunkIndex < this.chunk.length && (o == t.length || this.chunkEnd(h.chunkIndex) < t[o].from) && (!r || i > this.chunkEnd(h.chunkIndex) || s < this.chunkPos[h.chunkIndex]) && f.addChunk(this.chunkPos[h.chunkIndex], this.chunk[h.chunkIndex]) ? h.nextChunk() : ((!r || i > h.to || s < h.from || r(h.from, h.to, h.value)) && (f.addInner(h.from, h.to, h.value) || a.push(W.create(h.from, h.to, h.value))), h.next());
    return f.finishInner(this.nextLayer.isEmpty && !a.length ? I.empty : this.nextLayer.update({ add: a, filter: r, filterFrom: i, filterTo: s }));
  }
  /**
  Map this range set through a set of changes, return the new set.
  */
  map(e) {
    if (e.empty || this.isEmpty)
      return this;
    let t = [], n = [], i = -1;
    for (let r = 0; r < this.chunk.length; r++) {
      let h = this.chunkPos[r], o = this.chunk[r], a = e.touchesRange(h, h + o.length);
      if (a === !1)
        i = Math.max(i, o.maxPoint), t.push(o), n.push(e.mapPos(h));
      else if (a === !0) {
        let { mapped: f, pos: c } = o.map(h, e);
        f && (i = Math.max(i, f.maxPoint), t.push(f), n.push(c));
      }
    }
    let s = this.nextLayer.map(e);
    return t.length == 0 ? s : new I(n, t, s || I.empty, i);
  }
  /**
  Iterate over the ranges that touch the region `from` to `to`,
  calling `f` for each. There is no guarantee that the ranges will
  be reported in any specific order. When the callback returns
  `false`, iteration stops.
  */
  between(e, t, n) {
    if (!this.isEmpty) {
      for (let i = 0; i < this.chunk.length; i++) {
        let s = this.chunkPos[i], r = this.chunk[i];
        if (t >= s && e <= s + r.length && r.between(s, e - s, t - s, n) === !1)
          return;
      }
      this.nextLayer.between(e, t, n);
    }
  }
  /**
  Iterate over the ranges in this set, in order, including all
  ranges that end at or after `from`.
  */
  iter(e = 0) {
    return U.from([this]).goto(e);
  }
  /**
  @internal
  */
  get isEmpty() {
    return this.nextLayer == this;
  }
  /**
  Iterate over the ranges in a collection of sets, in order,
  starting from `from`.
  */
  static iter(e, t = 0) {
    return U.from(e).goto(t);
  }
  /**
  Iterate over two groups of sets, calling methods on `comparator`
  to notify it of possible differences.
  */
  static compare(e, t, n, i, s = -1) {
    let r = e.filter((c) => c.maxPoint > 0 || !c.isEmpty && c.maxPoint >= s), h = t.filter((c) => c.maxPoint > 0 || !c.isEmpty && c.maxPoint >= s), o = ye(r, h, n), a = new D(r, o, s), f = new D(h, o, s);
    n.iterGaps((c, d, g) => ke(a, c, f, d, g, i)), n.empty && n.length == 0 && ke(a, 0, f, 0, 0, i);
  }
  /**
  Compare the contents of two groups of range sets, returning true
  if they are equivalent in the given range.
  */
  static eq(e, t, n = 0, i) {
    i == null && (i = 999999999);
    let s = e.filter((f) => !f.isEmpty && t.indexOf(f) < 0), r = t.filter((f) => !f.isEmpty && e.indexOf(f) < 0);
    if (s.length != r.length)
      return !1;
    if (!s.length)
      return !0;
    let h = ye(s, r), o = new D(s, h, 0).goto(n), a = new D(r, h, 0).goto(n);
    for (; ; ) {
      if (o.to != a.to || !ge(o.active, a.active) || o.point && (!a.point || !o.point.eq(a.point)))
        return !1;
      if (o.to > i)
        return !0;
      o.next(), a.next();
    }
  }
  /**
  Iterate over a group of range sets at the same time, notifying
  the iterator about the ranges covering every given piece of
  content. Returns the open count (see
  [`SpanIterator.span`](https://codemirror.net/6/docs/ref/#state.SpanIterator.span)) at the end
  of the iteration.
  */
  static spans(e, t, n, i, s = -1) {
    let r = new D(e, null, s).goto(t), h = t, o = r.openStart;
    for (; ; ) {
      let a = Math.min(r.to, n);
      if (r.point) {
        let f = r.activeForPoint(r.to), c = r.pointFrom < t ? f.length + 1 : r.point.startSide < 0 ? f.length : Math.min(f.length, o);
        i.point(h, a, r.point, f, c, r.pointRank), o = Math.min(r.openEnd(a), f.length);
      } else a > h && (i.span(h, a, r.active, o), o = r.openEnd(a));
      if (r.to > n)
        return o + (r.point && r.to > n ? 1 : 0);
      h = r.to, r.next();
    }
  }
  /**
  Create a range set for the given range or array of ranges. By
  default, this expects the ranges to be _sorted_ (by start
  position and, if two start at the same position,
  `value.startSide`). You can pass `true` as second argument to
  cause the method to sort them.
  */
  static of(e, t = !1) {
    let n = new te();
    for (let i of e instanceof W ? [e] : t ? et(e) : e)
      n.add(i.from, i.to, i.value);
    return n.finish();
  }
  /**
  Join an array of range sets into a single set.
  */
  static join(e) {
    if (!e.length)
      return I.empty;
    let t = e[e.length - 1];
    for (let n = e.length - 2; n >= 0; n--)
      for (let i = e[n]; i != I.empty; i = i.nextLayer)
        t = new I(i.chunkPos, i.chunk, t, Math.max(i.maxPoint, t.maxPoint));
    return t;
  }
}
I.empty = /* @__PURE__ */ new I([], [], null, -1);
function et(l) {
  if (l.length > 1)
    for (let e = l[0], t = 1; t < l.length; t++) {
      let n = l[t];
      if (de(e, n) > 0)
        return l.slice().sort(de);
      e = n;
    }
  return l;
}
u(et, "lazySort");
I.empty.nextLayer = I.empty;
class te {
  static {
    u(this, "RangeSetBuilder");
  }
  finishChunk(e) {
    this.chunks.push(new we(this.from, this.to, this.value, this.maxPoint)), this.chunkPos.push(this.chunkStart), this.chunkStart = -1, this.setMaxPoint = Math.max(this.setMaxPoint, this.maxPoint), this.maxPoint = -1, e && (this.from = [], this.to = [], this.value = []);
  }
  /**
  Create an empty builder.
  */
  constructor() {
    this.chunks = [], this.chunkPos = [], this.chunkStart = -1, this.last = null, this.lastFrom = -1e9, this.lastTo = -1e9, this.from = [], this.to = [], this.value = [], this.maxPoint = -1, this.setMaxPoint = -1, this.nextLayer = null;
  }
  /**
  Add a range. Ranges should be added in sorted (by `from` and
  `value.startSide`) order.
  */
  add(e, t, n) {
    this.addInner(e, t, n) || (this.nextLayer || (this.nextLayer = new te())).add(e, t, n);
  }
  /**
  @internal
  */
  addInner(e, t, n) {
    let i = e - this.lastTo || n.startSide - this.last.endSide;
    if (i <= 0 && (e - this.lastFrom || n.startSide - this.last.startSide) < 0)
      throw new Error("Ranges must be added sorted by `from` position and `startSide`");
    return i < 0 ? !1 : (this.from.length == 250 && this.finishChunk(!0), this.chunkStart < 0 && (this.chunkStart = e), this.from.push(e - this.chunkStart), this.to.push(t - this.chunkStart), this.last = n, this.lastFrom = e, this.lastTo = t, this.value.push(n), n.point && (this.maxPoint = Math.max(this.maxPoint, t - e)), !0);
  }
  /**
  @internal
  */
  addChunk(e, t) {
    if ((e - this.lastTo || t.value[0].startSide - this.last.endSide) < 0)
      return !1;
    this.from.length && this.finishChunk(!0), this.setMaxPoint = Math.max(this.setMaxPoint, t.maxPoint), this.chunks.push(t), this.chunkPos.push(e);
    let n = t.value.length - 1;
    return this.last = t.value[n], this.lastFrom = t.from[n] + e, this.lastTo = t.to[n] + e, !0;
  }
  /**
  Finish the range set. Returns the new set. The builder can't be
  used anymore after this has been called.
  */
  finish() {
    return this.finishInner(I.empty);
  }
  /**
  @internal
  */
  finishInner(e) {
    if (this.from.length && this.finishChunk(!1), this.chunks.length == 0)
      return e;
    let t = I.create(this.chunkPos, this.chunks, this.nextLayer ? this.nextLayer.finishInner(e) : e, this.setMaxPoint);
    return this.from = null, t;
  }
}
function ye(l, e, t) {
  let n = /* @__PURE__ */ new Map();
  for (let s of l)
    for (let r = 0; r < s.chunk.length; r++)
      s.chunk[r].maxPoint <= 0 && n.set(s.chunk[r], s.chunkPos[r]);
  let i = /* @__PURE__ */ new Set();
  for (let s of e)
    for (let r = 0; r < s.chunk.length; r++) {
      let h = n.get(s.chunk[r]);
      h != null && (t ? t.mapPos(h) : h) == s.chunkPos[r] && !t?.touchesRange(h, h + s.chunk[r].length) && i.add(s.chunk[r]);
    }
  return i;
}
u(ye, "findSharedChunks");
class Le {
  static {
    u(this, "LayerCursor");
  }
  constructor(e, t, n, i = 0) {
    this.layer = e, this.skip = t, this.minPoint = n, this.rank = i;
  }
  get startSide() {
    return this.value ? this.value.startSide : 0;
  }
  get endSide() {
    return this.value ? this.value.endSide : 0;
  }
  goto(e, t = -1e9) {
    return this.chunkIndex = this.rangeIndex = 0, this.gotoInner(e, t, !1), this;
  }
  gotoInner(e, t, n) {
    for (; this.chunkIndex < this.layer.chunk.length; ) {
      let i = this.layer.chunk[this.chunkIndex];
      if (!(this.skip && this.skip.has(i) || this.layer.chunkEnd(this.chunkIndex) < e || i.maxPoint < this.minPoint))
        break;
      this.chunkIndex++, n = !1;
    }
    if (this.chunkIndex < this.layer.chunk.length) {
      let i = this.layer.chunk[this.chunkIndex].findIndex(e - this.layer.chunkPos[this.chunkIndex], t, !0);
      (!n || this.rangeIndex < i) && this.setRangeIndex(i);
    }
    this.next();
  }
  forward(e, t) {
    (this.to - e || this.endSide - t) < 0 && this.gotoInner(e, t, !0);
  }
  next() {
    for (; ; )
      if (this.chunkIndex == this.layer.chunk.length) {
        this.from = this.to = 1e9, this.value = null;
        break;
      } else {
        let e = this.layer.chunkPos[this.chunkIndex], t = this.layer.chunk[this.chunkIndex], n = e + t.from[this.rangeIndex];
        if (this.from = n, this.to = e + t.to[this.rangeIndex], this.value = t.value[this.rangeIndex], this.setRangeIndex(this.rangeIndex + 1), this.minPoint < 0 || this.value.point && this.to - this.from >= this.minPoint)
          break;
      }
  }
  setRangeIndex(e) {
    if (e == this.layer.chunk[this.chunkIndex].value.length) {
      if (this.chunkIndex++, this.skip)
        for (; this.chunkIndex < this.layer.chunk.length && this.skip.has(this.layer.chunk[this.chunkIndex]); )
          this.chunkIndex++;
      this.rangeIndex = 0;
    } else
      this.rangeIndex = e;
  }
  nextChunk() {
    this.chunkIndex++, this.rangeIndex = 0, this.next();
  }
  compare(e) {
    return this.from - e.from || this.startSide - e.startSide || this.rank - e.rank || this.to - e.to || this.endSide - e.endSide;
  }
}
class U {
  static {
    u(this, "HeapCursor");
  }
  constructor(e) {
    this.heap = e;
  }
  static from(e, t = null, n = -1) {
    let i = [];
    for (let s = 0; s < e.length; s++)
      for (let r = e[s]; !r.isEmpty; r = r.nextLayer)
        r.maxPoint >= n && i.push(new Le(r, t, n, s));
    return i.length == 1 ? i[0] : new U(i);
  }
  get startSide() {
    return this.value ? this.value.startSide : 0;
  }
  goto(e, t = -1e9) {
    for (let n of this.heap)
      n.goto(e, t);
    for (let n = this.heap.length >> 1; n >= 0; n--)
      se(this.heap, n);
    return this.next(), this;
  }
  forward(e, t) {
    for (let n of this.heap)
      n.forward(e, t);
    for (let n = this.heap.length >> 1; n >= 0; n--)
      se(this.heap, n);
    (this.to - e || this.value.endSide - t) < 0 && this.next();
  }
  next() {
    if (this.heap.length == 0)
      this.from = this.to = 1e9, this.value = null, this.rank = -1;
    else {
      let e = this.heap[0];
      this.from = e.from, this.to = e.to, this.value = e.value, this.rank = e.rank, e.value && e.next(), se(this.heap, 0);
    }
  }
}
function se(l, e) {
  for (let t = l[e]; ; ) {
    let n = (e << 1) + 1;
    if (n >= l.length)
      break;
    let i = l[n];
    if (n + 1 < l.length && i.compare(l[n + 1]) >= 0 && (i = l[n + 1], n++), t.compare(i) < 0)
      break;
    l[n] = t, l[e] = i, e = n;
  }
}
u(se, "heapBubble");
class D {
  static {
    u(this, "SpanCursor");
  }
  constructor(e, t, n) {
    this.minPoint = n, this.active = [], this.activeTo = [], this.activeRank = [], this.minActive = -1, this.point = null, this.pointFrom = 0, this.pointRank = 0, this.to = -1e9, this.endSide = 0, this.openStart = -1, this.cursor = U.from(e, t, n);
  }
  goto(e, t = -1e9) {
    return this.cursor.goto(e, t), this.active.length = this.activeTo.length = this.activeRank.length = 0, this.minActive = -1, this.to = e, this.endSide = t, this.openStart = -1, this.next(), this;
  }
  forward(e, t) {
    for (; this.minActive > -1 && (this.activeTo[this.minActive] - e || this.active[this.minActive].endSide - t) < 0; )
      this.removeActive(this.minActive);
    this.cursor.forward(e, t);
  }
  removeActive(e) {
    K(this.active, e), K(this.activeTo, e), K(this.activeRank, e), this.minActive = Se(this.active, this.activeTo);
  }
  addActive(e) {
    let t = 0, { value: n, to: i, rank: s } = this.cursor;
    for (; t < this.activeRank.length && (s - this.activeRank[t] || i - this.activeTo[t]) > 0; )
      t++;
    Q(this.active, t, n), Q(this.activeTo, t, i), Q(this.activeRank, t, s), e && Q(e, t, this.cursor.from), this.minActive = Se(this.active, this.activeTo);
  }
  // After calling this, if `this.point` != null, the next range is a
  // point. Otherwise, it's a regular range, covered by `this.active`.
  next() {
    let e = this.to, t = this.point;
    this.point = null;
    let n = this.openStart < 0 ? [] : null;
    for (; ; ) {
      let i = this.minActive;
      if (i > -1 && (this.activeTo[i] - this.cursor.from || this.active[i].endSide - this.cursor.startSide) < 0) {
        if (this.activeTo[i] > e) {
          this.to = this.activeTo[i], this.endSide = this.active[i].endSide;
          break;
        }
        this.removeActive(i), n && K(n, i);
      } else if (this.cursor.value)
        if (this.cursor.from > e) {
          this.to = this.cursor.from, this.endSide = this.cursor.startSide;
          break;
        } else {
          let s = this.cursor.value;
          if (!s.point)
            this.addActive(n), this.cursor.next();
          else if (t && this.cursor.to == this.to && this.cursor.from < this.cursor.to)
            this.cursor.next();
          else {
            this.point = s, this.pointFrom = this.cursor.from, this.pointRank = this.cursor.rank, this.to = this.cursor.to, this.endSide = s.endSide, this.cursor.next(), this.forward(this.to, this.endSide);
            break;
          }
        }
      else {
        this.to = this.endSide = 1e9;
        break;
      }
    }
    if (n) {
      this.openStart = 0;
      for (let i = n.length - 1; i >= 0 && n[i] < e; i--)
        this.openStart++;
    }
  }
  activeForPoint(e) {
    if (!this.active.length)
      return this.active;
    let t = [];
    for (let n = this.active.length - 1; n >= 0 && !(this.activeRank[n] < this.pointRank); n--)
      (this.activeTo[n] > e || this.activeTo[n] == e && this.active[n].endSide >= this.point.endSide) && t.push(this.active[n]);
    return t.reverse();
  }
  openEnd(e) {
    let t = 0;
    for (let n = this.activeTo.length - 1; n >= 0 && this.activeTo[n] > e; n--)
      t++;
    return t;
  }
}
function ke(l, e, t, n, i, s) {
  l.goto(e), t.goto(n);
  let r = n + i, h = n, o = n - e;
  for (; ; ) {
    let a = l.to + o - t.to, f = a || l.endSide - t.endSide, c = f < 0 ? l.to + o : t.to, d = Math.min(c, r);
    if (l.point || t.point ? l.point && t.point && (l.point == t.point || l.point.eq(t.point)) && ge(l.activeForPoint(l.to), t.activeForPoint(t.to)) || s.comparePoint(h, d, l.point, t.point) : d > h && !ge(l.active, t.active) && s.compareRange(h, d, l.active, t.active), c > r)
      break;
    (a || l.openEnd != t.openEnd) && s.boundChange && s.boundChange(c), h = c, f <= 0 && l.next(), f >= 0 && t.next();
  }
}
u(ke, "compare");
function ge(l, e) {
  if (l.length != e.length)
    return !1;
  for (let t = 0; t < l.length; t++)
    if (l[t] != e[t] && !l[t].eq(e[t]))
      return !1;
  return !0;
}
u(ge, "sameValues");
function K(l, e) {
  for (let t = e, n = l.length - 1; t < n; t++)
    l[t] = l[t + 1];
  l.pop();
}
u(K, "remove");
function Q(l, e, t) {
  for (let n = l.length - 1; n >= e; n--)
    l[n + 1] = l[n];
  l[e] = t;
}
u(Q, "insert");
function Se(l, e) {
  let t = -1, n = 1e9;
  for (let i = 0; i < e.length; i++)
    (e[i] - n || l[i].endSide - l[t].endSide) < 0 && (t = i, n = e[i]);
  return t;
}
u(Se, "findMinIndex");
function ot(l, e, t = l.length) {
  let n = 0;
  for (let i = 0; i < t && i < l.length; )
    l.charCodeAt(i) == 9 ? (n += e - n % e, i++) : (n++, i = Z(l, i));
  return n;
}
u(ot, "countColumn");
function at(l, e, t, n) {
  for (let i = 0, s = 0; ; ) {
    if (s >= e)
      return i;
    if (i == l.length)
      break;
    s += l.charCodeAt(i) == 9 ? t - s % t : 1, i = Z(l, i);
  }
  return n === !0 ? -1 : l.length;
}
u(at, "findColumn");
export {
  G as Annotation,
  Ge as AnnotationType,
  M as ChangeDesc,
  k as ChangeSet,
  T as CharCategory,
  ne as Compartment,
  p as EditorSelection,
  v as EditorState,
  E as Facet,
  $e as Line,
  b as MapMode,
  lt as Prec,
  W as Range,
  I as RangeSet,
  te as RangeSetBuilder,
  ee as RangeValue,
  F as SelectionRange,
  P as StateEffect,
  He as StateEffectType,
  V as StateField,
  y as Text,
  A as Transaction,
  it as codePointAt,
  rt as codePointSize,
  ht as combineConfig,
  ot as countColumn,
  Z as findClusterBreak,
  at as findColumn,
  st as fromCodePoint
};
