class g {
  constructor(e, t, s, n) {
    this._uri = e, this._languageId = t, this._version = s, this._content = n, this._lineOffsets = void 0;
  }
  get uri() {
    return this._uri;
  }
  get languageId() {
    return this._languageId;
  }
  get version() {
    return this._version;
  }
  getText(e) {
    if (e) {
      const t = this.offsetAt(e.start), s = this.offsetAt(e.end);
      return this._content.substring(t, s);
    }
    return this._content;
  }
  update(e, t) {
    for (const s of e)
      if (g.isIncremental(s)) {
        const n = L(s.range), i = this.offsetAt(n.start), f = this.offsetAt(n.end);
        this._content = this._content.substring(0, i) + s.text + this._content.substring(f, this._content.length);
        const l = Math.max(n.start.line, 0), u = Math.max(n.end.line, 0);
        let c = this._lineOffsets;
        const h = w(s.text, !1, i);
        if (u - l === h.length)
          for (let o = 0, d = h.length; o < d; o++)
            c[o + l + 1] = h[o];
        else
          h.length < 1e4 ? c.splice(l + 1, u - l, ...h) : this._lineOffsets = c = c.slice(0, l + 1).concat(h, c.slice(u + 1));
        const a = s.text.length - (f - i);
        if (a !== 0)
          for (let o = l + 1 + h.length, d = c.length; o < d; o++)
            c[o] = c[o] + a;
      } else if (g.isFull(s))
        this._content = s.text, this._lineOffsets = void 0;
      else
        throw new Error("Unknown change event received");
    this._version = t;
  }
  getLineOffsets() {
    return this._lineOffsets === void 0 && (this._lineOffsets = w(this._content, !0)), this._lineOffsets;
  }
  positionAt(e) {
    e = Math.max(Math.min(e, this._content.length), 0);
    const t = this.getLineOffsets();
    let s = 0, n = t.length;
    if (n === 0)
      return { line: 0, character: e };
    for (; s < n; ) {
      const f = Math.floor((s + n) / 2);
      t[f] > e ? n = f : s = f + 1;
    }
    const i = s - 1;
    return e = this.ensureBeforeEOL(e, t[i]), { line: i, character: e - t[i] };
  }
  offsetAt(e) {
    const t = this.getLineOffsets();
    if (e.line >= t.length)
      return this._content.length;
    if (e.line < 0)
      return 0;
    const s = t[e.line];
    if (e.character <= 0)
      return s;
    const n = e.line + 1 < t.length ? t[e.line + 1] : this._content.length, i = Math.min(s + e.character, n);
    return this.ensureBeforeEOL(i, s);
  }
  ensureBeforeEOL(e, t) {
    for (; e > t && p(this._content.charCodeAt(e - 1)); )
      e--;
    return e;
  }
  get lineCount() {
    return this.getLineOffsets().length;
  }
  static isIncremental(e) {
    const t = e;
    return t != null && typeof t.text == "string" && t.range !== void 0 && (t.rangeLength === void 0 || typeof t.rangeLength == "number");
  }
  static isFull(e) {
    const t = e;
    return t != null && typeof t.text == "string" && t.range === void 0 && t.rangeLength === void 0;
  }
}
var _;
(function(r) {
  function e(n, i, f, l) {
    return new g(n, i, f, l);
  }
  r.create = e;
  function t(n, i, f) {
    if (n instanceof g)
      return n.update(i, f), n;
    throw new Error("TextDocument.update: document must be created by TextDocument.create");
  }
  r.update = t;
  function s(n, i) {
    const f = n.getText(), l = O(i.map(x), (h, a) => {
      const o = h.range.start.line - a.range.start.line;
      return o === 0 ? h.range.start.character - a.range.start.character : o;
    });
    let u = 0;
    const c = [];
    for (const h of l) {
      const a = n.offsetAt(h.range.start);
      if (a < u)
        throw new Error("Overlapping edit");
      a > u && c.push(f.substring(u, a)), h.newText.length && c.push(h.newText), u = n.offsetAt(h.range.end);
    }
    return c.push(f.substr(u)), c.join("");
  }
  r.applyEdits = s;
})(_ || (_ = {}));
function O(r, e) {
  if (r.length <= 1)
    return r;
  const t = r.length / 2 | 0, s = r.slice(0, t), n = r.slice(t);
  O(s, e), O(n, e);
  let i = 0, f = 0, l = 0;
  for (; i < s.length && f < n.length; )
    e(s[i], n[f]) <= 0 ? r[l++] = s[i++] : r[l++] = n[f++];
  for (; i < s.length; )
    r[l++] = s[i++];
  for (; f < n.length; )
    r[l++] = n[f++];
  return r;
}
function w(r, e, t = 0) {
  const s = e ? [t] : [];
  for (let n = 0; n < r.length; n++) {
    const i = r.charCodeAt(n);
    p(i) && (i === 13 && n + 1 < r.length && r.charCodeAt(n + 1) === 10 && n++, s.push(t + n + 1));
  }
  return s;
}
function p(r) {
  return r === 13 || r === 10;
}
function L(r) {
  const e = r.start, t = r.end;
  return e.line > t.line || e.line === t.line && e.character > t.character ? { start: t, end: e } : r;
}
function x(r) {
  const e = L(r.range);
  return e !== r.range ? { newText: r.newText, range: e } : r;
}
export {
  _ as TextDocument
};
