import { ds as l, dt as p, du as f, dv as v } from "./index-BJZQSU1O.mjs";
const k = 120 * 1e3;
class m {
  constructor(e) {
    this._defaults = e, this._worker = null, this._client = null, this._idleCheckInterval = window.setInterval(() => this._checkIfIdle(), 30 * 1e3), this._lastUsedTime = 0, this._configChangeListener = this._defaults.onDidChange(() => this._stopWorker());
  }
  _stopWorker() {
    this._worker && (this._worker.dispose(), this._worker = null), this._client = null;
  }
  dispose() {
    clearInterval(this._idleCheckInterval), this._configChangeListener.dispose(), this._stopWorker();
  }
  _checkIfIdle() {
    if (!this._worker)
      return;
    Date.now() - this._lastUsedTime > k && this._stopWorker();
  }
  _getClient() {
    return this._lastUsedTime = Date.now(), this._client || (this._worker = l.createWebWorker({
      // module that exports the create() method and returns a `CSSWorker` instance
      moduleId: this._defaults.languageId,
      label: this._defaults.languageId,
      // passed in to the create() method
      createData: {
        languageId: this._defaults.languageId
      }
    }), this._client = this._worker.getProxy()), this._client;
  }
  getLanguageServiceWorker(...e) {
    const s = e?.filter(Boolean);
    return this._getClient().then((r) => (s && s?.length && this._worker && this._worker.withSyncedResources(s), r));
  }
}
function C(n, e, s) {
  let r = null;
  return (...o) => {
    r && clearTimeout(r), r = setTimeout(() => {
      r && clearTimeout(r), r = null, n?.(...o);
    }, e);
  };
}
class w {
  constructor(e, s, r) {
    this._languageId = e, this._worker = s, this._defaults = r, this._disposables = [], this._listener = /* @__PURE__ */ Object.create(null);
    const o = (t) => {
      let i = t.getLanguageId();
      i === this._languageId && (this._listener[t.uri.toString()] = t.onDidChangeContent(C(() => {
        this._doValidate(t.uri, i);
      }, 500)), this._doValidate(t.uri, i));
    }, a = (t) => {
      l.setModelMarkers(t, this._languageId, []);
      let i = t.uri.toString(), d = this._listener[i];
      d && (d.dispose(), delete this._listener[i]);
    };
    this._disposables.push(l.onDidCreateModel(o)), this._disposables.push(l.onWillDisposeModel(a)), this._disposables.push(l.onDidChangeModelLanguage((t) => {
      a(t.model), o(t.model);
    })), this._disposables.push(this._defaults.onDidChange((t) => {
      l.getModels().forEach((i) => {
        i.getLanguageId() === this._languageId && (a(i), o(i));
      });
    })), this._disposables.push({
      dispose: () => {
        for (let t in this._listener)
          this._listener[t].dispose();
      }
    }), l.getModels().forEach(o);
  }
  dispose() {
    this._disposables.forEach((e) => e && e.dispose()), this._disposables = [];
  }
  _doValidate(e, s) {
    this._worker(e).then((r) => {
      var o;
      let a = ((o = l.getModel(e)) === null || o === void 0 ? void 0 : o.getValue()) || "";
      return typeof this._defaults.preprocessCode == "function" && (a = this._defaults.preprocessCode(a)), r.doValidation(a);
    }).then((r) => {
      const o = r.map((t) => b(e, t));
      let a = l.getModel(e);
      a && a.getLanguageId() === s && l.setModelMarkers(a, s, o);
    }).then(void 0, (r) => {
      console.error(r);
    });
  }
}
function I(n) {
  return f.Error;
}
function b(n, e) {
  return {
    severity: I(),
    startLineNumber: e.startLine,
    startColumn: e.startColumn,
    endLineNumber: e.endLine,
    endColumn: e.endColumn,
    message: e.message,
    code: void 0,
    source: "dt-sql-parser"
  };
}
class y {
  constructor(e, s) {
    this._worker = e, this._defaults = s;
  }
  get triggerCharacters() {
    return Array.isArray(this._defaults.triggerCharacters) ? this._defaults.triggerCharacters : [".", " "];
  }
  provideCompletionItems(e, s, r, o) {
    const a = e.uri;
    return this._worker(a).then((t) => {
      var i;
      let d = ((i = l.getModel(a)) === null || i === void 0 ? void 0 : i.getValue()) || "";
      return typeof this._defaults.preprocessCode == "function" && (d = this._defaults.preprocessCode(d)), t.doCompletionWithEntities(d, s);
    }).then(([t, i]) => this._defaults.completionService(e, s, r, t, i)).then((t) => {
      const i = e.getWordUntilPosition(s), d = new p(s.lineNumber, i.startColumn, s.lineNumber, i.endColumn);
      return {
        suggestions: (Array.isArray(t) ? t : t.suggestions).map((u) => {
          var h, g;
          return Object.assign(Object.assign({}, u), { insertText: (h = u.insertText) !== null && h !== void 0 ? h : typeof u.label == "string" ? u.label : u.label.label, range: (g = u.range) !== null && g !== void 0 ? g : d });
        }),
        dispose: Array.isArray(t) ? void 0 : t.dispose,
        incomplete: Array.isArray(t) ? void 0 : t.incomplete
      };
    });
  }
}
function W(n) {
  const e = [], s = [], r = new m(n);
  e.push(r);
  const o = (...t) => r.getLanguageServiceWorker(...t);
  function a() {
    const { languageId: t, modeConfiguration: i } = n;
    _(s), i.diagnostics && s.push(new w(t, o, n)), i.completionItems.enable && s.push(v.registerCompletionItemProvider(t, new y(o, n)));
  }
  return a(), e.push(c(s)), c(e);
}
function c(n) {
  return { dispose: () => _(n) };
}
function _(n) {
  for (var e; n.length; )
    (e = n.pop()) === null || e === void 0 || e.dispose();
}
export {
  W as setupLanguageMode
};
//# sourceMappingURL=setupLanguageMode-0AefDMmp.mjs.map
