var pe = Object.defineProperty;
var c = (t, e) => pe(t, "name", { value: e, configurable: !0 });
import { EditorView as S, ViewPlugin as ee, getPanel as P, Decoration as m, showPanel as te, runScopeHandlers as ge } from "../../../../../@codemirror_view@6.36.4/node_modules/@codemirror/view/dist/index.js";
import { StateField as ne, StateEffect as D, Prec as xe, EditorSelection as x, Facet as re, combineConfig as ie, codePointAt as Se, codePointSize as ye, fromCodePoint as Me, CharCategory as g, EditorState as be, RangeSetBuilder as ve, findClusterBreak as se } from "../../../../../@codemirror_state@6.5.2/node_modules/@codemirror/state/dist/index.js";
import u from "../../../../../crelt@1.0.6/node_modules/crelt/index.js";
const U = typeof String.prototype.normalize == "function" ? (t) => t.normalize("NFKD") : (t) => t;
class C {
  static {
    c(this, "SearchCursor");
  }
  /**
  Create a text cursor. The query is the search string, `from` to
  `to` provides the region to search.
  
  When `normalize` is given, it will be called, on both the query
  string and the content it is matched against, before comparing.
  You can, for example, create a case-insensitive search by
  passing `s => s.toLowerCase()`.
  
  Text is always normalized with
  [`.normalize("NFKD")`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize)
  (when supported).
  */
  constructor(e, n, r = 0, i = e.length, s, l) {
    this.test = l, this.value = { from: 0, to: 0 }, this.done = !1, this.matches = [], this.buffer = "", this.bufferPos = 0, this.iter = e.iterRange(r, i), this.bufferStart = r, this.normalize = s ? (o) => s(U(o)) : U, this.query = this.normalize(n);
  }
  peek() {
    if (this.bufferPos == this.buffer.length) {
      if (this.bufferStart += this.buffer.length, this.iter.next(), this.iter.done)
        return -1;
      this.bufferPos = 0, this.buffer = this.iter.value;
    }
    return Se(this.buffer, this.bufferPos);
  }
  /**
  Look for the next match. Updates the iterator's
  [`value`](https://codemirror.net/6/docs/ref/#search.SearchCursor.value) and
  [`done`](https://codemirror.net/6/docs/ref/#search.SearchCursor.done) properties. Should be called
  at least once before using the cursor.
  */
  next() {
    for (; this.matches.length; )
      this.matches.pop();
    return this.nextOverlapping();
  }
  /**
  The `next` method will ignore matches that partially overlap a
  previous match. This method behaves like `next`, but includes
  such matches.
  */
  nextOverlapping() {
    for (; ; ) {
      let e = this.peek();
      if (e < 0)
        return this.done = !0, this;
      let n = Me(e), r = this.bufferStart + this.bufferPos;
      this.bufferPos += ye(e);
      let i = this.normalize(n);
      if (i.length)
        for (let s = 0, l = r; ; s++) {
          let o = i.charCodeAt(s), a = this.match(o, l, this.bufferPos + this.bufferStart);
          if (s == i.length - 1) {
            if (a)
              return this.value = a, this;
            break;
          }
          l == r && s < n.length && n.charCodeAt(s) == o && l++;
        }
    }
  }
  match(e, n, r) {
    let i = null;
    for (let s = 0; s < this.matches.length; s += 2) {
      let l = this.matches[s], o = !1;
      this.query.charCodeAt(l) == e && (l == this.query.length - 1 ? i = { from: this.matches[s + 1], to: r } : (this.matches[s]++, o = !0)), o || (this.matches.splice(s, 2), s -= 2);
    }
    return this.query.charCodeAt(0) == e && (this.query.length == 1 ? i = { from: n, to: r } : this.matches.push(1, n)), i && this.test && !this.test(i.from, i.to, this.buffer, this.bufferStart) && (i = null), i;
  }
}
typeof Symbol < "u" && (C.prototype[Symbol.iterator] = function() {
  return this;
});
const le = { from: -1, to: -1, match: /* @__PURE__ */ /.*/.exec("") }, V = "gm" + (/x/.unicode == null ? "" : "u");
class oe {
  static {
    c(this, "RegExpCursor");
  }
  /**
  Create a cursor that will search the given range in the given
  document. `query` should be the raw pattern (as you'd pass it to
  `new RegExp`).
  */
  constructor(e, n, r, i = 0, s = e.length) {
    if (this.text = e, this.to = s, this.curLine = "", this.done = !1, this.value = le, /\\[sWDnr]|\n|\r|\[\^/.test(n))
      return new ce(e, n, r, i, s);
    this.re = new RegExp(n, V + (r?.ignoreCase ? "i" : "")), this.test = r?.test, this.iter = e.iter();
    let l = e.lineAt(i);
    this.curLineStart = l.from, this.matchPos = I(e, i), this.getLine(this.curLineStart);
  }
  getLine(e) {
    this.iter.next(e), this.iter.lineBreak ? this.curLine = "" : (this.curLine = this.iter.value, this.curLineStart + this.curLine.length > this.to && (this.curLine = this.curLine.slice(0, this.to - this.curLineStart)), this.iter.next());
  }
  nextLine() {
    this.curLineStart = this.curLineStart + this.curLine.length + 1, this.curLineStart > this.to ? this.curLine = "" : this.getLine(0);
  }
  /**
  Move to the next match, if there is one.
  */
  next() {
    for (let e = this.matchPos - this.curLineStart; ; ) {
      this.re.lastIndex = e;
      let n = this.matchPos <= this.to && this.re.exec(this.curLine);
      if (n) {
        let r = this.curLineStart + n.index, i = r + n[0].length;
        if (this.matchPos = I(this.text, i + (r == i ? 1 : 0)), r == this.curLineStart + this.curLine.length && this.nextLine(), (r < i || r > this.value.to) && (!this.test || this.test(r, i, n)))
          return this.value = { from: r, to: i, match: n }, this;
        e = this.matchPos - this.curLineStart;
      } else if (this.curLineStart + this.curLine.length < this.to)
        this.nextLine(), e = 0;
      else
        return this.done = !0, this;
    }
  }
}
const z = /* @__PURE__ */ new WeakMap();
class v {
  static {
    c(this, "FlattenedDoc");
  }
  constructor(e, n) {
    this.from = e, this.text = n;
  }
  get to() {
    return this.from + this.text.length;
  }
  static get(e, n, r) {
    let i = z.get(e);
    if (!i || i.from >= r || i.to <= n) {
      let o = new v(n, e.sliceString(n, r));
      return z.set(e, o), o;
    }
    if (i.from == n && i.to == r)
      return i;
    let { text: s, from: l } = i;
    return l > n && (s = e.sliceString(n, l) + s, l = n), i.to < r && (s += e.sliceString(i.to, r)), z.set(e, new v(l, s)), new v(n, s.slice(n - l, r - l));
  }
}
class ce {
  static {
    c(this, "MultilineRegExpCursor");
  }
  constructor(e, n, r, i, s) {
    this.text = e, this.to = s, this.done = !1, this.value = le, this.matchPos = I(e, i), this.re = new RegExp(n, V + (r?.ignoreCase ? "i" : "")), this.test = r?.test, this.flat = v.get(e, i, this.chunkEnd(
      i + 5e3
      /* Chunk.Base */
    ));
  }
  chunkEnd(e) {
    return e >= this.to ? this.to : this.text.lineAt(e).to;
  }
  next() {
    for (; ; ) {
      let e = this.re.lastIndex = this.matchPos - this.flat.from, n = this.re.exec(this.flat.text);
      if (n && !n[0] && n.index == e && (this.re.lastIndex = e + 1, n = this.re.exec(this.flat.text)), n) {
        let r = this.flat.from + n.index, i = r + n[0].length;
        if ((this.flat.to >= this.to || n.index + n[0].length <= this.flat.text.length - 10) && (!this.test || this.test(r, i, n)))
          return this.value = { from: r, to: i, match: n }, this.matchPos = I(this.text, i + (r == i ? 1 : 0)), this;
      }
      if (this.flat.to == this.to)
        return this.done = !0, this;
      this.flat = v.get(this.text, this.flat.from, this.chunkEnd(this.flat.from + this.flat.text.length * 2));
    }
  }
}
typeof Symbol < "u" && (oe.prototype[Symbol.iterator] = ce.prototype[Symbol.iterator] = function() {
  return this;
});
function Ce(t) {
  try {
    return new RegExp(t, V), !0;
  } catch {
    return !1;
  }
}
c(Ce, "validRegExp");
function I(t, e) {
  if (e >= t.length)
    return e;
  let n = t.lineAt(e), r;
  for (; e < n.to && (r = n.text.charCodeAt(e - n.from)) >= 56320 && r < 57344; )
    e++;
  return e;
}
c(I, "toCharEnd");
function B(t) {
  let e = String(t.state.doc.lineAt(t.state.selection.main.head).number), n = u("input", { class: "cm-textfield", name: "line", value: e }), r = u("form", {
    class: "cm-gotoLine",
    onkeydown: /* @__PURE__ */ c((s) => {
      s.keyCode == 27 ? (s.preventDefault(), t.dispatch({ effects: W.of(!1) }), t.focus()) : s.keyCode == 13 && (s.preventDefault(), i());
    }, "onkeydown"),
    onsubmit: /* @__PURE__ */ c((s) => {
      s.preventDefault(), i();
    }, "onsubmit")
  }, u("label", t.state.phrase("Go to line"), ": ", n), " ", u("button", { class: "cm-button", type: "submit" }, t.state.phrase("go")), u("button", {
    name: "close",
    onclick: /* @__PURE__ */ c(() => {
      t.dispatch({ effects: W.of(!1) }), t.focus();
    }, "onclick"),
    "aria-label": t.state.phrase("close"),
    type: "button"
  }, ["×"]));
  function i() {
    let s = /^([+-])?(\d+)?(:\d+)?(%)?$/.exec(n.value);
    if (!s)
      return;
    let { state: l } = t, o = l.doc.lineAt(l.selection.main.head), [, a, h, f, p] = s, F = f ? +f.slice(1) : 0, L = h ? +h : o.number;
    if (h && p) {
      let Q = L / 100;
      a && (Q = Q * (a == "-" ? -1 : 1) + o.number / l.doc.lines), L = Math.round(l.doc.lines * Q);
    } else h && a && (L = L * (a == "-" ? -1 : 1) + o.number);
    let j = l.doc.line(Math.max(1, Math.min(l.doc.lines, L))), J = x.cursor(j.from + Math.max(0, Math.min(F, j.length)));
    t.dispatch({
      effects: [W.of(!1), S.scrollIntoView(J.from, { y: "center" })],
      selection: J
    }), t.focus();
  }
  return c(i, "go"), { dom: r };
}
c(B, "createLineDialog");
const W = /* @__PURE__ */ D.define(), X = /* @__PURE__ */ ne.define({
  create() {
    return !0;
  },
  update(t, e) {
    for (let n of e.effects)
      n.is(W) && (t = n.value);
    return t;
  },
  provide: /* @__PURE__ */ c((t) => te.from(t, (e) => e ? B : null), "provide")
}), ke = /* @__PURE__ */ c((t) => {
  let e = P(t, B);
  if (!e) {
    let n = [W.of(!0)];
    t.state.field(X, !1) == null && n.push(D.appendConfig.of([X, Le])), t.dispatch({ effects: n }), e = P(t, B);
  }
  return e && e.dom.querySelector("input").select(), !0;
}, "gotoLine"), Le = /* @__PURE__ */ S.baseTheme({
  ".cm-panel.cm-gotoLine": {
    padding: "2px 6px 4px",
    position: "relative",
    "& label": { fontSize: "80%" },
    "& [name=close]": {
      position: "absolute",
      top: "0",
      bottom: "0",
      right: "4px",
      backgroundColor: "inherit",
      border: "none",
      font: "inherit",
      padding: "0"
    }
  }
}), We = {
  highlightWordAroundCursor: !1,
  minSelectionLength: 1,
  maxMatches: 100,
  wholeWords: !1
}, Ae = /* @__PURE__ */ re.define({
  combine(t) {
    return ie(t, We, {
      highlightWordAroundCursor: /* @__PURE__ */ c((e, n) => e || n, "highlightWordAroundCursor"),
      minSelectionLength: Math.min,
      maxMatches: Math.min
    });
  }
});
function Ze(t) {
  return [Re, qe];
}
c(Ze, "highlightSelectionMatches");
const De = /* @__PURE__ */ m.mark({ class: "cm-selectionMatch" }), Ee = /* @__PURE__ */ m.mark({ class: "cm-selectionMatch cm-selectionMatch-main" });
function Y(t, e, n, r) {
  return (n == 0 || t(e.sliceDoc(n - 1, n)) != g.Word) && (r == e.doc.length || t(e.sliceDoc(r, r + 1)) != g.Word);
}
c(Y, "insideWordBoundaries");
function Fe(t, e, n, r) {
  return t(e.sliceDoc(n, n + 1)) == g.Word && t(e.sliceDoc(r - 1, r)) == g.Word;
}
c(Fe, "insideWord");
const qe = /* @__PURE__ */ ee.fromClass(class {
  constructor(t) {
    this.decorations = this.getDeco(t);
  }
  update(t) {
    (t.selectionSet || t.docChanged || t.viewportChanged) && (this.decorations = this.getDeco(t.view));
  }
  getDeco(t) {
    let e = t.state.facet(Ae), { state: n } = t, r = n.selection;
    if (r.ranges.length > 1)
      return m.none;
    let i = r.main, s, l = null;
    if (i.empty) {
      if (!e.highlightWordAroundCursor)
        return m.none;
      let a = n.wordAt(i.head);
      if (!a)
        return m.none;
      l = n.charCategorizer(i.head), s = n.sliceDoc(a.from, a.to);
    } else {
      let a = i.to - i.from;
      if (a < e.minSelectionLength || a > 200)
        return m.none;
      if (e.wholeWords) {
        if (s = n.sliceDoc(i.from, i.to), l = n.charCategorizer(i.head), !(Y(l, n, i.from, i.to) && Fe(l, n, i.from, i.to)))
          return m.none;
      } else if (s = n.sliceDoc(i.from, i.to), !s)
        return m.none;
    }
    let o = [];
    for (let a of t.visibleRanges) {
      let h = new C(n.doc, s, a.from, a.to);
      for (; !h.next().done; ) {
        let { from: f, to: p } = h.value;
        if ((!l || Y(l, n, f, p)) && (i.empty && f <= i.from && p >= i.to ? o.push(Ee.range(f, p)) : (f >= i.to || p <= i.from) && o.push(De.range(f, p)), o.length > e.maxMatches))
          return m.none;
      }
    }
    return m.set(o);
  }
}, {
  decorations: /* @__PURE__ */ c((t) => t.decorations, "decorations")
}), Re = /* @__PURE__ */ S.baseTheme({
  ".cm-selectionMatch": { backgroundColor: "#99ff7780" },
  ".cm-searchMatch .cm-selectionMatch": { backgroundColor: "transparent" }
}), Pe = /* @__PURE__ */ c(({ state: t, dispatch: e }) => {
  let { selection: n } = t, r = x.create(n.ranges.map((i) => t.wordAt(i.head) || x.cursor(i.head)), n.mainIndex);
  return r.eq(n) ? !1 : (e(t.update({ selection: r })), !0);
}, "selectWord");
function Ie(t, e) {
  let { main: n, ranges: r } = t.selection, i = t.wordAt(n.head), s = i && i.from == n.from && i.to == n.to;
  for (let l = !1, o = new C(t.doc, e, r[r.length - 1].to); ; )
    if (o.next(), o.done) {
      if (l)
        return null;
      o = new C(t.doc, e, 0, Math.max(0, r[r.length - 1].from - 1)), l = !0;
    } else {
      if (l && r.some((a) => a.from == o.value.from))
        continue;
      if (s) {
        let a = t.wordAt(o.value.from);
        if (!a || a.from != o.value.from || a.to != o.value.to)
          continue;
      }
      return o.value;
    }
}
c(Ie, "findNextOccurrence");
const Te = /* @__PURE__ */ c(({ state: t, dispatch: e }) => {
  let { ranges: n } = t.selection;
  if (n.some((s) => s.from === s.to))
    return Pe({ state: t, dispatch: e });
  let r = t.sliceDoc(n[0].from, n[0].to);
  if (t.selection.ranges.some((s) => t.sliceDoc(s.from, s.to) != r))
    return !1;
  let i = Ie(t, r);
  return i ? (e(t.update({
    selection: t.selection.addRange(x.range(i.from, i.to), !1),
    effects: S.scrollIntoView(i.to)
  })), !0) : !1;
}, "selectNextOccurrence"), k = /* @__PURE__ */ re.define({
  combine(t) {
    return ie(t, {
      top: !1,
      caseSensitive: !1,
      literal: !1,
      regexp: !1,
      wholeWord: !1,
      createPanel: /* @__PURE__ */ c((e) => new Ke(e), "createPanel"),
      scrollToMatch: /* @__PURE__ */ c((e) => S.scrollIntoView(e), "scrollToMatch")
    });
  }
});
class ae {
  static {
    c(this, "SearchQuery");
  }
  /**
  Create a query object.
  */
  constructor(e) {
    this.search = e.search, this.caseSensitive = !!e.caseSensitive, this.literal = !!e.literal, this.regexp = !!e.regexp, this.replace = e.replace || "", this.valid = !!this.search && (!this.regexp || Ce(this.search)), this.unquoted = this.unquote(this.search), this.wholeWord = !!e.wholeWord;
  }
  /**
  @internal
  */
  unquote(e) {
    return this.literal ? e : e.replace(/\\([nrt\\])/g, (n, r) => r == "n" ? `
` : r == "r" ? "\r" : r == "t" ? "	" : "\\");
  }
  /**
  Compare this query to another query.
  */
  eq(e) {
    return this.search == e.search && this.replace == e.replace && this.caseSensitive == e.caseSensitive && this.regexp == e.regexp && this.wholeWord == e.wholeWord;
  }
  /**
  @internal
  */
  create() {
    return this.regexp ? new Qe(this) : new $e(this);
  }
  /**
  Get a search cursor for this query, searching through the given
  range in the given state.
  */
  getCursor(e, n = 0, r) {
    let i = e.doc ? e : be.create({ doc: e });
    return r == null && (r = i.doc.length), this.regexp ? b(this, i, n, r) : M(this, i, n, r);
  }
}
class he {
  static {
    c(this, "QueryType");
  }
  constructor(e) {
    this.spec = e;
  }
}
function M(t, e, n, r) {
  return new C(e.doc, t.unquoted, n, r, t.caseSensitive ? void 0 : (i) => i.toLowerCase(), t.wholeWord ? we(e.doc, e.charCategorizer(e.selection.main.head)) : void 0);
}
c(M, "stringCursor");
function we(t, e) {
  return (n, r, i, s) => ((s > n || s + i.length < r) && (s = Math.max(0, n - 2), i = t.sliceString(s, Math.min(t.length, r + 2))), (e(T(i, n - s)) != g.Word || e(w(i, n - s)) != g.Word) && (e(w(i, r - s)) != g.Word || e(T(i, r - s)) != g.Word));
}
c(we, "stringWordTest");
class $e extends he {
  static {
    c(this, "StringQuery");
  }
  constructor(e) {
    super(e);
  }
  nextMatch(e, n, r) {
    let i = M(this.spec, e, r, e.doc.length).nextOverlapping();
    if (i.done) {
      let s = Math.min(e.doc.length, n + this.spec.unquoted.length);
      i = M(this.spec, e, 0, s).nextOverlapping();
    }
    return i.done || i.value.from == n && i.value.to == r ? null : i.value;
  }
  // Searching in reverse is, rather than implementing an inverted search
  // cursor, done by scanning chunk after chunk forward.
  prevMatchInRange(e, n, r) {
    for (let i = r; ; ) {
      let s = Math.max(n, i - 1e4 - this.spec.unquoted.length), l = M(this.spec, e, s, i), o = null;
      for (; !l.nextOverlapping().done; )
        o = l.value;
      if (o)
        return o;
      if (s == n)
        return null;
      i -= 1e4;
    }
  }
  prevMatch(e, n, r) {
    let i = this.prevMatchInRange(e, 0, n);
    return i || (i = this.prevMatchInRange(e, Math.max(0, r - this.spec.unquoted.length), e.doc.length)), i && (i.from != n || i.to != r) ? i : null;
  }
  getReplacement(e) {
    return this.spec.unquote(this.spec.replace);
  }
  matchAll(e, n) {
    let r = M(this.spec, e, 0, e.doc.length), i = [];
    for (; !r.next().done; ) {
      if (i.length >= n)
        return null;
      i.push(r.value);
    }
    return i;
  }
  highlight(e, n, r, i) {
    let s = M(this.spec, e, Math.max(0, n - this.spec.unquoted.length), Math.min(r + this.spec.unquoted.length, e.doc.length));
    for (; !s.next().done; )
      i(s.value.from, s.value.to);
  }
}
function b(t, e, n, r) {
  return new oe(e.doc, t.search, {
    ignoreCase: !t.caseSensitive,
    test: t.wholeWord ? Oe(e.charCategorizer(e.selection.main.head)) : void 0
  }, n, r);
}
c(b, "regexpCursor");
function T(t, e) {
  return t.slice(se(t, e, !1), e);
}
c(T, "charBefore");
function w(t, e) {
  return t.slice(e, se(t, e));
}
c(w, "charAfter");
function Oe(t) {
  return (e, n, r) => !r[0].length || (t(T(r.input, r.index)) != g.Word || t(w(r.input, r.index)) != g.Word) && (t(w(r.input, r.index + r[0].length)) != g.Word || t(T(r.input, r.index + r[0].length)) != g.Word);
}
c(Oe, "regexpWordTest");
class Qe extends he {
  static {
    c(this, "RegExpQuery");
  }
  nextMatch(e, n, r) {
    let i = b(this.spec, e, r, e.doc.length).next();
    return i.done && (i = b(this.spec, e, 0, n).next()), i.done ? null : i.value;
  }
  prevMatchInRange(e, n, r) {
    for (let i = 1; ; i++) {
      let s = Math.max(
        n,
        r - i * 1e4
        /* FindPrev.ChunkSize */
      ), l = b(this.spec, e, s, r), o = null;
      for (; !l.next().done; )
        o = l.value;
      if (o && (s == n || o.from > s + 10))
        return o;
      if (s == n)
        return null;
    }
  }
  prevMatch(e, n, r) {
    return this.prevMatchInRange(e, 0, n) || this.prevMatchInRange(e, r, e.doc.length);
  }
  getReplacement(e) {
    return this.spec.unquote(this.spec.replace).replace(/\$([$&]|\d+)/g, (n, r) => {
      if (r == "&")
        return e.match[0];
      if (r == "$")
        return "$";
      for (let i = r.length; i > 0; i--) {
        let s = +r.slice(0, i);
        if (s > 0 && s < e.match.length)
          return e.match[s] + r.slice(i);
      }
      return n;
    });
  }
  matchAll(e, n) {
    let r = b(this.spec, e, 0, e.doc.length), i = [];
    for (; !r.next().done; ) {
      if (i.length >= n)
        return null;
      i.push(r.value);
    }
    return i;
  }
  highlight(e, n, r, i) {
    let s = b(this.spec, e, Math.max(
      0,
      n - 250
      /* RegExp.HighlightMargin */
    ), Math.min(r + 250, e.doc.length));
    for (; !s.next().done; )
      i(s.value.from, s.value.to);
  }
}
const A = /* @__PURE__ */ D.define(), H = /* @__PURE__ */ D.define(), y = /* @__PURE__ */ ne.define({
  create(t) {
    return new _(N(t).create(), null);
  },
  update(t, e) {
    for (let n of e.effects)
      n.is(A) ? t = new _(n.value.create(), t.panel) : n.is(H) && (t = new _(t.query, n.value ? K : null));
    return t;
  },
  provide: /* @__PURE__ */ c((t) => te.from(t, (e) => e.panel), "provide")
});
class _ {
  static {
    c(this, "SearchState");
  }
  constructor(e, n) {
    this.query = e, this.panel = n;
  }
}
const ze = /* @__PURE__ */ m.mark({ class: "cm-searchMatch" }), _e = /* @__PURE__ */ m.mark({ class: "cm-searchMatch cm-searchMatch-selected" }), Be = /* @__PURE__ */ ee.fromClass(class {
  constructor(t) {
    this.view = t, this.decorations = this.highlight(t.state.field(y));
  }
  update(t) {
    let e = t.state.field(y);
    (e != t.startState.field(y) || t.docChanged || t.selectionSet || t.viewportChanged) && (this.decorations = this.highlight(e));
  }
  highlight({ query: t, panel: e }) {
    if (!e || !t.spec.valid)
      return m.none;
    let { view: n } = this, r = new ve();
    for (let i = 0, s = n.visibleRanges, l = s.length; i < l; i++) {
      let { from: o, to: a } = s[i];
      for (; i < l - 1 && a > s[i + 1].from - 2 * 250; )
        a = s[++i].to;
      t.highlight(n.state, o, a, (h, f) => {
        let p = n.state.selection.ranges.some((F) => F.from == h && F.to == f);
        r.add(h, f, p ? _e : ze);
      });
    }
    return r.finish();
  }
}, {
  decorations: /* @__PURE__ */ c((t) => t.decorations, "decorations")
});
function E(t) {
  return (e) => {
    let n = e.state.field(y, !1);
    return n && n.query.spec.valid ? t(e, n) : de(e);
  };
}
c(E, "searchCommand");
const $ = /* @__PURE__ */ E((t, { query: e }) => {
  let { to: n } = t.state.selection.main, r = e.nextMatch(t.state, n, n);
  if (!r)
    return !1;
  let i = x.single(r.from, r.to), s = t.state.facet(k);
  return t.dispatch({
    selection: i,
    effects: [G(t, r), s.scrollToMatch(i.main, t)],
    userEvent: "select.search"
  }), fe(t), !0;
}), O = /* @__PURE__ */ E((t, { query: e }) => {
  let { state: n } = t, { from: r } = n.selection.main, i = e.prevMatch(n, r, r);
  if (!i)
    return !1;
  let s = x.single(i.from, i.to), l = t.state.facet(k);
  return t.dispatch({
    selection: s,
    effects: [G(t, i), l.scrollToMatch(s.main, t)],
    userEvent: "select.search"
  }), fe(t), !0;
}), Ne = /* @__PURE__ */ E((t, { query: e }) => {
  let n = e.matchAll(t.state, 1e3);
  return !n || !n.length ? !1 : (t.dispatch({
    selection: x.create(n.map((r) => x.range(r.from, r.to))),
    userEvent: "select.search.matches"
  }), !0);
}), Ve = /* @__PURE__ */ c(({ state: t, dispatch: e }) => {
  let n = t.selection;
  if (n.ranges.length > 1 || n.main.empty)
    return !1;
  let { from: r, to: i } = n.main, s = [], l = 0;
  for (let o = new C(t.doc, t.sliceDoc(r, i)); !o.next().done; ) {
    if (s.length > 1e3)
      return !1;
    o.value.from == r && (l = s.length), s.push(x.range(o.value.from, o.value.to));
  }
  return e(t.update({
    selection: x.create(s, l),
    userEvent: "select.search.matches"
  })), !0;
}, "selectSelectionMatches"), Z = /* @__PURE__ */ E((t, { query: e }) => {
  let { state: n } = t, { from: r, to: i } = n.selection.main;
  if (n.readOnly)
    return !1;
  let s = e.nextMatch(n, r, r);
  if (!s)
    return !1;
  let l = s, o = [], a, h, f = [];
  if (l.from == r && l.to == i && (h = n.toText(e.getReplacement(l)), o.push({ from: l.from, to: l.to, insert: h }), l = e.nextMatch(n, l.from, l.to), f.push(S.announce.of(n.phrase("replaced match on line $", n.doc.lineAt(r).number) + "."))), l) {
    let p = o.length == 0 || o[0].from >= s.to ? 0 : s.to - s.from - h.length;
    a = x.single(l.from - p, l.to - p), f.push(G(t, l)), f.push(n.facet(k).scrollToMatch(a.main, t));
  }
  return t.dispatch({
    changes: o,
    selection: a,
    effects: f,
    userEvent: "input.replace"
  }), !0;
}), He = /* @__PURE__ */ E((t, { query: e }) => {
  if (t.state.readOnly)
    return !1;
  let n = e.matchAll(t.state, 1e9).map((i) => {
    let { from: s, to: l } = i;
    return { from: s, to: l, insert: e.getReplacement(i) };
  });
  if (!n.length)
    return !1;
  let r = t.state.phrase("replaced $ matches", n.length) + ".";
  return t.dispatch({
    changes: n,
    effects: S.announce.of(r),
    userEvent: "input.replace.all"
  }), !0;
});
function K(t) {
  return t.state.facet(k).createPanel(t);
}
c(K, "createSearchPanel");
function N(t, e) {
  var n, r, i, s, l;
  let o = t.selection.main, a = o.empty || o.to > o.from + 100 ? "" : t.sliceDoc(o.from, o.to);
  if (e && !a)
    return e;
  let h = t.facet(k);
  return new ae({
    search: ((n = e?.literal) !== null && n !== void 0 ? n : h.literal) ? a : a.replace(/\n/g, "\\n"),
    caseSensitive: (r = e?.caseSensitive) !== null && r !== void 0 ? r : h.caseSensitive,
    literal: (i = e?.literal) !== null && i !== void 0 ? i : h.literal,
    regexp: (s = e?.regexp) !== null && s !== void 0 ? s : h.regexp,
    wholeWord: (l = e?.wholeWord) !== null && l !== void 0 ? l : h.wholeWord
  });
}
c(N, "defaultQuery");
function ue(t) {
  let e = P(t, K);
  return e && e.dom.querySelector("[main-field]");
}
c(ue, "getSearchInput");
function fe(t) {
  let e = ue(t);
  e && e == t.root.activeElement && e.select();
}
c(fe, "selectSearchInput");
const de = /* @__PURE__ */ c((t) => {
  let e = t.state.field(y, !1);
  if (e && e.panel) {
    let n = ue(t);
    if (n && n != t.root.activeElement) {
      let r = N(t.state, e.query.spec);
      r.valid && t.dispatch({ effects: A.of(r) }), n.focus(), n.select();
    }
  } else
    t.dispatch({ effects: [
      H.of(!0),
      e ? A.of(N(t.state, e.query.spec)) : D.appendConfig.of(je)
    ] });
  return !0;
}, "openSearchPanel"), me = /* @__PURE__ */ c((t) => {
  let e = t.state.field(y, !1);
  if (!e || !e.panel)
    return !1;
  let n = P(t, K);
  return n && n.dom.contains(t.root.activeElement) && t.focus(), t.dispatch({ effects: H.of(!1) }), !0;
}, "closeSearchPanel"), et = [
  { key: "Mod-f", run: de, scope: "editor search-panel" },
  { key: "F3", run: $, shift: O, scope: "editor search-panel", preventDefault: !0 },
  { key: "Mod-g", run: $, shift: O, scope: "editor search-panel", preventDefault: !0 },
  { key: "Escape", run: me, scope: "editor search-panel" },
  { key: "Mod-Shift-l", run: Ve },
  { key: "Mod-Alt-g", run: ke },
  { key: "Mod-d", run: Te, preventDefault: !0 }
];
class Ke {
  static {
    c(this, "SearchPanel");
  }
  constructor(e) {
    this.view = e;
    let n = this.query = e.state.field(y).query.spec;
    this.commit = this.commit.bind(this), this.searchField = u("input", {
      value: n.search,
      placeholder: d(e, "Find"),
      "aria-label": d(e, "Find"),
      class: "cm-textfield",
      name: "search",
      form: "",
      "main-field": "true",
      onchange: this.commit,
      onkeyup: this.commit
    }), this.replaceField = u("input", {
      value: n.replace,
      placeholder: d(e, "Replace"),
      "aria-label": d(e, "Replace"),
      class: "cm-textfield",
      name: "replace",
      form: "",
      onchange: this.commit,
      onkeyup: this.commit
    }), this.caseField = u("input", {
      type: "checkbox",
      name: "case",
      form: "",
      checked: n.caseSensitive,
      onchange: this.commit
    }), this.reField = u("input", {
      type: "checkbox",
      name: "re",
      form: "",
      checked: n.regexp,
      onchange: this.commit
    }), this.wordField = u("input", {
      type: "checkbox",
      name: "word",
      form: "",
      checked: n.wholeWord,
      onchange: this.commit
    });
    function r(i, s, l) {
      return u("button", { class: "cm-button", name: i, onclick: s, type: "button" }, l);
    }
    c(r, "button"), this.dom = u("div", { onkeydown: /* @__PURE__ */ c((i) => this.keydown(i), "onkeydown"), class: "cm-search" }, [
      this.searchField,
      r("next", () => $(e), [d(e, "next")]),
      r("prev", () => O(e), [d(e, "previous")]),
      r("select", () => Ne(e), [d(e, "all")]),
      u("label", null, [this.caseField, d(e, "match case")]),
      u("label", null, [this.reField, d(e, "regexp")]),
      u("label", null, [this.wordField, d(e, "by word")]),
      ...e.state.readOnly ? [] : [
        u("br"),
        this.replaceField,
        r("replace", () => Z(e), [d(e, "replace")]),
        r("replaceAll", () => He(e), [d(e, "replace all")])
      ],
      u("button", {
        name: "close",
        onclick: /* @__PURE__ */ c(() => me(e), "onclick"),
        "aria-label": d(e, "close"),
        type: "button"
      }, ["×"])
    ]);
  }
  commit() {
    let e = new ae({
      search: this.searchField.value,
      caseSensitive: this.caseField.checked,
      regexp: this.reField.checked,
      wholeWord: this.wordField.checked,
      replace: this.replaceField.value
    });
    e.eq(this.query) || (this.query = e, this.view.dispatch({ effects: A.of(e) }));
  }
  keydown(e) {
    ge(this.view, e, "search-panel") ? e.preventDefault() : e.keyCode == 13 && e.target == this.searchField ? (e.preventDefault(), (e.shiftKey ? O : $)(this.view)) : e.keyCode == 13 && e.target == this.replaceField && (e.preventDefault(), Z(this.view));
  }
  update(e) {
    for (let n of e.transactions)
      for (let r of n.effects)
        r.is(A) && !r.value.eq(this.query) && this.setQuery(r.value);
  }
  setQuery(e) {
    this.query = e, this.searchField.value = e.search, this.replaceField.value = e.replace, this.caseField.checked = e.caseSensitive, this.reField.checked = e.regexp, this.wordField.checked = e.wholeWord;
  }
  mount() {
    this.searchField.select();
  }
  get pos() {
    return 80;
  }
  get top() {
    return this.view.state.facet(k).top;
  }
}
function d(t, e) {
  return t.state.phrase(e);
}
c(d, "phrase");
const q = 30, R = /[\s\.,:;?!]/;
function G(t, { from: e, to: n }) {
  let r = t.state.doc.lineAt(e), i = t.state.doc.lineAt(n).to, s = Math.max(r.from, e - q), l = Math.min(i, n + q), o = t.state.sliceDoc(s, l);
  if (s != r.from) {
    for (let a = 0; a < q; a++)
      if (!R.test(o[a + 1]) && R.test(o[a])) {
        o = o.slice(a);
        break;
      }
  }
  if (l != i) {
    for (let a = o.length - 1; a > o.length - q; a--)
      if (!R.test(o[a - 1]) && R.test(o[a])) {
        o = o.slice(0, a);
        break;
      }
  }
  return S.announce.of(`${t.state.phrase("current match")}. ${o} ${t.state.phrase("on line")} ${r.number}.`);
}
c(G, "announceMatch");
const Ge = /* @__PURE__ */ S.baseTheme({
  ".cm-panel.cm-search": {
    padding: "2px 6px 4px",
    position: "relative",
    "& [name=close]": {
      position: "absolute",
      top: "0",
      right: "4px",
      backgroundColor: "inherit",
      border: "none",
      font: "inherit",
      padding: 0,
      margin: 0
    },
    "& input, & button, & label": {
      margin: ".2em .6em .2em 0"
    },
    "& input[type=checkbox]": {
      marginRight: ".2em"
    },
    "& label": {
      fontSize: "80%",
      whiteSpace: "pre"
    }
  },
  "&light .cm-searchMatch": { backgroundColor: "#ffff0054" },
  "&dark .cm-searchMatch": { backgroundColor: "#00ffff8a" },
  "&light .cm-searchMatch-selected": { backgroundColor: "#ff6a0054" },
  "&dark .cm-searchMatch-selected": { backgroundColor: "#ff00ff8a" }
}), je = [
  y,
  /* @__PURE__ */ xe.low(Be),
  Ge
];
export {
  oe as RegExpCursor,
  C as SearchCursor,
  ae as SearchQuery,
  me as closeSearchPanel,
  $ as findNext,
  O as findPrevious,
  ke as gotoLine,
  Ze as highlightSelectionMatches,
  de as openSearchPanel,
  He as replaceAll,
  Z as replaceNext,
  et as searchKeymap,
  Ne as selectMatches,
  Te as selectNextOccurrence,
  Ve as selectSelectionMatches,
  A as setSearchQuery
};
