var H = Object.defineProperty;
var l = (t, e) => H(t, "name", { value: e, configurable: !0 });
import { ViewPlugin as V, EditorView as T, hoverTooltip as _, getPanel as q, Decoration as y, showPanel as W, WidgetType as j, logException as K } from "../../../../../@codemirror_view@6.36.4/node_modules/@codemirror/view/dist/index.js";
import { Facet as N, StateField as U, StateEffect as R, combineConfig as Y, RangeSetBuilder as Z } from "../../../../../@codemirror_state@6.5.2/node_modules/@codemirror/state/dist/index.js";
import b from "../../../../../crelt@1.0.6/node_modules/crelt/index.js";
class L {
  static {
    l(this, "SelectedDiagnostic");
  }
  constructor(e, s, i) {
    this.from = e, this.to = s, this.diagnostic = i;
  }
}
class x {
  static {
    l(this, "LintState");
  }
  constructor(e, s, i) {
    this.diagnostics = e, this.panel = s, this.selected = i;
  }
  static init(e, s, i) {
    let o = i.facet(w).markerFilter;
    o && (e = o(e, i));
    let n = e.slice().sort((c, u) => c.from - u.from || c.to - u.to), a = new Z(), r = [], f = 0;
    for (let c = 0; ; ) {
      let u = c == n.length ? null : n[c];
      if (!u && !r.length)
        break;
      let h, g;
      for (r.length ? (h = f, g = r.reduce((d, k) => Math.min(d, k.to), u && u.from > h ? u.from : 1e8)) : (h = u.from, g = u.to, r.push(u), c++); c < n.length; ) {
        let d = n[c];
        if (d.from == h && (d.to > d.from || d.to == h))
          r.push(d), c++, g = Math.min(d.to, g);
        else {
          g = Math.min(d.from, g);
          break;
        }
      }
      let I = ce(r);
      if (r.some((d) => d.from == d.to || d.from == d.to - 1 && i.doc.lineAt(d.from).to == d.from))
        a.add(h, h, y.widget({
          widget: new ne(I),
          diagnostics: r.slice()
        }));
      else {
        let d = r.reduce((k, A) => A.markClass ? k + " " + A.markClass : k, "");
        a.add(h, g, y.mark({
          class: "cm-lintRange cm-lintRange-" + I + d,
          diagnostics: r.slice(),
          inclusiveEnd: r.some((k) => k.to > g)
        }));
      }
      f = g;
      for (let d = 0; d < r.length; d++)
        r[d].to <= f && r.splice(d--, 1);
    }
    let m = a.finish();
    return new x(m, s, v(m));
  }
}
function v(t, e = null, s = 0) {
  let i = null;
  return t.between(s, 1e9, (o, n, { spec: a }) => {
    if (!(e && a.diagnostics.indexOf(e) < 0))
      if (!i)
        i = new L(o, n, e || a.diagnostics[0]);
      else {
        if (a.diagnostics.indexOf(i.diagnostic) < 0)
          return !1;
        i = new L(i.from, n, i.diagnostic);
      }
  }), i;
}
l(v, "findDiagnostic");
function G(t, e) {
  let s = e.pos, i = e.end || s, o = t.state.facet(w).hideOn(t, s, i);
  if (o != null)
    return o;
  let n = t.startState.doc.lineAt(e.pos);
  return !!(t.effects.some((a) => a.is(P)) || t.changes.touchesRange(n.from, Math.max(n.to, i)));
}
l(G, "hideTooltip");
function F(t, e) {
  return t.field(p, !1) ? e : e.concat(R.appendConfig.of(z));
}
l(F, "maybeEnableLint");
function J(t, e) {
  return {
    effects: F(t, [P.of(e)])
  };
}
l(J, "setDiagnostics");
const P = /* @__PURE__ */ R.define(), D = /* @__PURE__ */ R.define(), E = /* @__PURE__ */ R.define(), p = /* @__PURE__ */ U.define({
  create() {
    return new x(y.none, null, null);
  },
  update(t, e) {
    if (e.docChanged && t.diagnostics.size) {
      let s = t.diagnostics.map(e.changes), i = null, o = t.panel;
      if (t.selected) {
        let n = e.changes.mapPos(t.selected.from, 1);
        i = v(s, t.selected.diagnostic, n) || v(s, null, n);
      }
      !s.size && o && e.state.facet(w).autoPanel && (o = null), t = new x(s, o, i);
    }
    for (let s of e.effects)
      if (s.is(P)) {
        let i = e.state.facet(w).autoPanel ? s.value.length ? C.open : null : t.panel;
        t = x.init(s.value, i, e.state);
      } else s.is(D) ? t = new x(t.diagnostics, s.value ? C.open : null, t.selected) : s.is(E) && (t = new x(t.diagnostics, t.panel, s.value));
    return t;
  },
  provide: /* @__PURE__ */ l((t) => [
    W.from(t, (e) => e.panel),
    T.decorations.from(t, (e) => e.diagnostics)
  ], "provide")
}), Q = /* @__PURE__ */ y.mark({ class: "cm-lintRange cm-lintRange-active" });
function X(t, e, s) {
  let { diagnostics: i } = t.state.field(p), o, n = -1, a = -1;
  i.between(e - (s < 0 ? 1 : 0), e + (s > 0 ? 1 : 0), (f, m, { spec: c }) => {
    if (e >= f && e <= m && (f == m || (e > f || s > 0) && (e < m || s < 0)))
      return o = c.diagnostics, n = f, a = m, !1;
  });
  let r = t.state.facet(w).tooltipFilter;
  return o && r && (o = r(o, t.state)), o ? {
    pos: n,
    end: a,
    above: t.state.doc.lineAt(n).to < a,
    create() {
      return { dom: ee(t, o) };
    }
  } : null;
}
l(X, "lintTooltip");
function ee(t, e) {
  return b("ul", { class: "cm-tooltip-lint" }, e.map((s) => $(t, s, !1)));
}
l(ee, "diagnosticsTooltip");
const te = /* @__PURE__ */ l((t) => {
  let e = t.state.field(p, !1);
  (!e || !e.panel) && t.dispatch({ effects: F(t.state, [D.of(!0)]) });
  let s = q(t, C.open);
  return s && s.dom.querySelector(".cm-panel-lint ul").focus(), !0;
}, "openLintPanel"), M = /* @__PURE__ */ l((t) => {
  let e = t.state.field(p, !1);
  return !e || !e.panel ? !1 : (t.dispatch({ effects: D.of(!1) }), !0);
}, "closeLintPanel"), ie = /* @__PURE__ */ l((t) => {
  let e = t.state.field(p, !1);
  if (!e)
    return !1;
  let s = t.state.selection.main, i = e.diagnostics.iter(s.to + 1);
  return !i.value && (i = e.diagnostics.iter(0), !i.value || i.from == s.from && i.to == s.to) ? !1 : (t.dispatch({ selection: { anchor: i.from, head: i.to }, scrollIntoView: !0 }), !0);
}, "nextDiagnostic"), ue = [
  { key: "Mod-Shift-m", run: te, preventDefault: !0 },
  { key: "F8", run: ie }
], se = /* @__PURE__ */ V.fromClass(class {
  constructor(t) {
    this.view = t, this.timeout = -1, this.set = !0;
    let { delay: e } = t.state.facet(w);
    this.lintTime = Date.now() + e, this.run = this.run.bind(this), this.timeout = setTimeout(this.run, e);
  }
  run() {
    clearTimeout(this.timeout);
    let t = Date.now();
    if (t < this.lintTime - 10)
      this.timeout = setTimeout(this.run, this.lintTime - t);
    else {
      this.set = !1;
      let { state: e } = this.view, { sources: s } = e.facet(w);
      s.length && oe(s.map((i) => Promise.resolve(i(this.view))), (i) => {
        this.view.state.doc == e.doc && this.view.dispatch(J(this.view.state, i.reduce((o, n) => o.concat(n))));
      }, (i) => {
        K(this.view.state, i);
      });
    }
  }
  update(t) {
    let e = t.state.facet(w);
    (t.docChanged || e != t.startState.facet(w) || e.needsRefresh && e.needsRefresh(t)) && (this.lintTime = Date.now() + e.delay, this.set || (this.set = !0, this.timeout = setTimeout(this.run, e.delay)));
  }
  force() {
    this.set && (this.lintTime = Date.now(), this.run());
  }
  destroy() {
    clearTimeout(this.timeout);
  }
});
function oe(t, e, s) {
  let i = [], o = -1;
  for (let n of t)
    n.then((a) => {
      i.push(a), clearTimeout(o), i.length == t.length ? e(i) : o = setTimeout(() => e(i), 200);
    }, s);
}
l(oe, "batchResults");
const w = /* @__PURE__ */ N.define({
  combine(t) {
    return Object.assign({ sources: t.map((e) => e.source).filter((e) => e != null) }, Y(t.map((e) => e.config), {
      delay: 750,
      markerFilter: null,
      tooltipFilter: null,
      needsRefresh: null,
      hideOn: /* @__PURE__ */ l(() => null, "hideOn")
    }, {
      needsRefresh: /* @__PURE__ */ l((e, s) => e ? s ? (i) => e(i) || s(i) : e : s, "needsRefresh")
    }));
  }
});
function ge(t, e = {}) {
  return [
    w.of({ source: t, config: e }),
    se,
    z
  ];
}
l(ge, "linter");
function O(t) {
  let e = [];
  if (t)
    e: for (let { name: s } of t) {
      for (let i = 0; i < s.length; i++) {
        let o = s[i];
        if (/[a-zA-Z]/.test(o) && !e.some((n) => n.toLowerCase() == o.toLowerCase())) {
          e.push(o);
          continue e;
        }
      }
      e.push("");
    }
  return e;
}
l(O, "assignKeys");
function $(t, e, s) {
  var i;
  let o = s ? O(e.actions) : [];
  return b("li", { class: "cm-diagnostic cm-diagnostic-" + e.severity }, b("span", { class: "cm-diagnosticText" }, e.renderMessage ? e.renderMessage(t) : e.message), (i = e.actions) === null || i === void 0 ? void 0 : i.map((n, a) => {
    let r = !1, f = /* @__PURE__ */ l((h) => {
      if (h.preventDefault(), r)
        return;
      r = !0;
      let g = v(t.state.field(p).diagnostics, e);
      g && n.apply(t, g.from, g.to);
    }, "click"), { name: m } = n, c = o[a] ? m.indexOf(o[a]) : -1, u = c < 0 ? m : [
      m.slice(0, c),
      b("u", m.slice(c, c + 1)),
      m.slice(c + 1)
    ];
    return b("button", {
      type: "button",
      class: "cm-diagnosticAction",
      onclick: f,
      onmousedown: f,
      "aria-label": ` Action: ${m}${c < 0 ? "" : ` (access key "${o[a]})"`}.`
    }, u);
  }), e.source && b("div", { class: "cm-diagnosticSource" }, e.source));
}
l($, "renderDiagnostic");
class ne extends j {
  static {
    l(this, "DiagnosticWidget");
  }
  constructor(e) {
    super(), this.sev = e;
  }
  eq(e) {
    return e.sev == this.sev;
  }
  toDOM() {
    return b("span", { class: "cm-lintPoint cm-lintPoint-" + this.sev });
  }
}
class B {
  static {
    l(this, "PanelItem");
  }
  constructor(e, s) {
    this.diagnostic = s, this.id = "item_" + Math.floor(Math.random() * 4294967295).toString(16), this.dom = $(e, s, !0), this.dom.id = this.id, this.dom.setAttribute("role", "option");
  }
}
class C {
  static {
    l(this, "LintPanel");
  }
  constructor(e) {
    this.view = e, this.items = [];
    let s = /* @__PURE__ */ l((o) => {
      if (o.keyCode == 27)
        M(this.view), this.view.focus();
      else if (o.keyCode == 38 || o.keyCode == 33)
        this.moveSelection((this.selectedIndex - 1 + this.items.length) % this.items.length);
      else if (o.keyCode == 40 || o.keyCode == 34)
        this.moveSelection((this.selectedIndex + 1) % this.items.length);
      else if (o.keyCode == 36)
        this.moveSelection(0);
      else if (o.keyCode == 35)
        this.moveSelection(this.items.length - 1);
      else if (o.keyCode == 13)
        this.view.focus();
      else if (o.keyCode >= 65 && o.keyCode <= 90 && this.selectedIndex >= 0) {
        let { diagnostic: n } = this.items[this.selectedIndex], a = O(n.actions);
        for (let r = 0; r < a.length; r++)
          if (a[r].toUpperCase().charCodeAt(0) == o.keyCode) {
            let f = v(this.view.state.field(p).diagnostics, n);
            f && n.actions[r].apply(e, f.from, f.to);
          }
      } else
        return;
      o.preventDefault();
    }, "onkeydown"), i = /* @__PURE__ */ l((o) => {
      for (let n = 0; n < this.items.length; n++)
        this.items[n].dom.contains(o.target) && this.moveSelection(n);
    }, "onclick");
    this.list = b("ul", {
      tabIndex: 0,
      role: "listbox",
      "aria-label": this.view.state.phrase("Diagnostics"),
      onkeydown: s,
      onclick: i
    }), this.dom = b("div", { class: "cm-panel-lint" }, this.list, b("button", {
      type: "button",
      name: "close",
      "aria-label": this.view.state.phrase("close"),
      onclick: /* @__PURE__ */ l(() => M(this.view), "onclick")
    }, "×")), this.update();
  }
  get selectedIndex() {
    let e = this.view.state.field(p).selected;
    if (!e)
      return -1;
    for (let s = 0; s < this.items.length; s++)
      if (this.items[s].diagnostic == e.diagnostic)
        return s;
    return -1;
  }
  update() {
    let { diagnostics: e, selected: s } = this.view.state.field(p), i = 0, o = !1, n = null, a = /* @__PURE__ */ new Set();
    for (e.between(0, this.view.state.doc.length, (r, f, { spec: m }) => {
      for (let c of m.diagnostics) {
        if (a.has(c))
          continue;
        a.add(c);
        let u = -1, h;
        for (let g = i; g < this.items.length; g++)
          if (this.items[g].diagnostic == c) {
            u = g;
            break;
          }
        u < 0 ? (h = new B(this.view, c), this.items.splice(i, 0, h), o = !0) : (h = this.items[u], u > i && (this.items.splice(i, u - i), o = !0)), s && h.diagnostic == s.diagnostic ? h.dom.hasAttribute("aria-selected") || (h.dom.setAttribute("aria-selected", "true"), n = h) : h.dom.hasAttribute("aria-selected") && h.dom.removeAttribute("aria-selected"), i++;
      }
    }); i < this.items.length && !(this.items.length == 1 && this.items[0].diagnostic.from < 0); )
      o = !0, this.items.pop();
    this.items.length == 0 && (this.items.push(new B(this.view, {
      from: -1,
      to: -1,
      severity: "info",
      message: this.view.state.phrase("No diagnostics")
    })), o = !0), n ? (this.list.setAttribute("aria-activedescendant", n.id), this.view.requestMeasure({
      key: this,
      read: /* @__PURE__ */ l(() => ({ sel: n.dom.getBoundingClientRect(), panel: this.list.getBoundingClientRect() }), "read"),
      write: /* @__PURE__ */ l(({ sel: r, panel: f }) => {
        let m = f.height / this.list.offsetHeight;
        r.top < f.top ? this.list.scrollTop -= (f.top - r.top) / m : r.bottom > f.bottom && (this.list.scrollTop += (r.bottom - f.bottom) / m);
      }, "write")
    })) : this.selectedIndex < 0 && this.list.removeAttribute("aria-activedescendant"), o && this.sync();
  }
  sync() {
    let e = this.list.firstChild;
    function s() {
      let i = e;
      e = i.nextSibling, i.remove();
    }
    l(s, "rm");
    for (let i of this.items)
      if (i.dom.parentNode == this.list) {
        for (; e != i.dom; )
          s();
        e = i.dom.nextSibling;
      } else
        this.list.insertBefore(i.dom, e);
    for (; e; )
      s();
  }
  moveSelection(e) {
    if (this.selectedIndex < 0)
      return;
    let s = this.view.state.field(p), i = v(s.diagnostics, this.items[e].diagnostic);
    i && this.view.dispatch({
      selection: { anchor: i.from, head: i.to },
      scrollIntoView: !0,
      effects: E.of(i)
    });
  }
  static open(e) {
    return new C(e);
  }
}
function le(t, e = 'viewBox="0 0 40 40"') {
  return `url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" ${e}>${encodeURIComponent(t)}</svg>')`;
}
l(le, "svg");
function S(t) {
  return le(`<path d="m0 2.5 l2 -1.5 l1 0 l2 1.5 l1 0" stroke="${t}" fill="none" stroke-width=".7"/>`, 'width="6" height="3"');
}
l(S, "underline");
const re = /* @__PURE__ */ T.baseTheme({
  ".cm-diagnostic": {
    padding: "3px 6px 3px 8px",
    marginLeft: "-1px",
    display: "block",
    whiteSpace: "pre-wrap"
  },
  ".cm-diagnostic-error": { borderLeft: "5px solid #d11" },
  ".cm-diagnostic-warning": { borderLeft: "5px solid orange" },
  ".cm-diagnostic-info": { borderLeft: "5px solid #999" },
  ".cm-diagnostic-hint": { borderLeft: "5px solid #66d" },
  ".cm-diagnosticAction": {
    font: "inherit",
    border: "none",
    padding: "2px 4px",
    backgroundColor: "#444",
    color: "white",
    borderRadius: "3px",
    marginLeft: "8px",
    cursor: "pointer"
  },
  ".cm-diagnosticSource": {
    fontSize: "70%",
    opacity: 0.7
  },
  ".cm-lintRange": {
    backgroundPosition: "left bottom",
    backgroundRepeat: "repeat-x",
    paddingBottom: "0.7px"
  },
  ".cm-lintRange-error": { backgroundImage: /* @__PURE__ */ S("#d11") },
  ".cm-lintRange-warning": { backgroundImage: /* @__PURE__ */ S("orange") },
  ".cm-lintRange-info": { backgroundImage: /* @__PURE__ */ S("#999") },
  ".cm-lintRange-hint": { backgroundImage: /* @__PURE__ */ S("#66d") },
  ".cm-lintRange-active": { backgroundColor: "#ffdd9980" },
  ".cm-tooltip-lint": {
    padding: 0,
    margin: 0
  },
  ".cm-lintPoint": {
    position: "relative",
    "&:after": {
      content: '""',
      position: "absolute",
      bottom: 0,
      left: "-2px",
      borderLeft: "3px solid transparent",
      borderRight: "3px solid transparent",
      borderBottom: "4px solid #d11"
    }
  },
  ".cm-lintPoint-warning": {
    "&:after": { borderBottomColor: "orange" }
  },
  ".cm-lintPoint-info": {
    "&:after": { borderBottomColor: "#999" }
  },
  ".cm-lintPoint-hint": {
    "&:after": { borderBottomColor: "#66d" }
  },
  ".cm-panel.cm-panel-lint": {
    position: "relative",
    "& ul": {
      maxHeight: "100px",
      overflowY: "auto",
      "& [aria-selected]": {
        backgroundColor: "#ddd",
        "& u": { textDecoration: "underline" }
      },
      "&:focus [aria-selected]": {
        background_fallback: "#bdf",
        backgroundColor: "Highlight",
        color_fallback: "white",
        color: "HighlightText"
      },
      "& u": { textDecoration: "none" },
      padding: 0,
      margin: 0
    },
    "& [name=close]": {
      position: "absolute",
      top: "0",
      right: "2px",
      background: "inherit",
      border: "none",
      font: "inherit",
      padding: 0,
      margin: 0
    }
  }
});
function ae(t) {
  return t == "error" ? 4 : t == "warning" ? 3 : t == "info" ? 2 : 1;
}
l(ae, "severityWeight");
function ce(t) {
  let e = "hint", s = 1;
  for (let i of t) {
    let o = ae(i.severity);
    o > s && (s = o, e = i.severity);
  }
  return e;
}
l(ce, "maxSeverity");
const z = [
  p,
  /* @__PURE__ */ T.decorations.compute([p], (t) => {
    let { selected: e, panel: s } = t.field(p);
    return !e || !s || e.from == e.to ? y.none : y.set([
      Q.range(e.from, e.to)
    ]);
  }),
  /* @__PURE__ */ _(X, { hideOn: G }),
  re
];
export {
  M as closeLintPanel,
  ue as lintKeymap,
  ge as linter,
  ie as nextDiagnostic,
  te as openLintPanel,
  J as setDiagnostics,
  P as setDiagnosticsEffect
};
