"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const index$1 = require("../../_vendor/@codemirror/autocomplete/dist-index.cjs");
const facet = require("./facet.cjs");
const snippets = require("./snippets.cjs");
const index = require("../../utils/index.cjs");
const START_CHARACTERS = ['"', "'", "(", ".", "@"];
const START_CHARACTERS_REGEX = /[.('"@]/;
const matchText = (context) => {
  let word = context.matchBefore(START_CHARACTERS_REGEX);
  if (!(word == null ? void 0 : word.text)) {
    word = context.matchBefore(/[$\w]+/);
  }
  if (!(word == null ? void 0 : word.text)) {
    word = context.matchBefore(/["'].*/);
  }
  return word;
};
const typescriptCompletionSource = async (context) => {
  var _a;
  const { worker } = context.state.facet(facet.typescriptWorkerFacet);
  const line = context.state.doc.lineAt(context.pos);
  const lineText = line.text;
  const importMatch = lineText.match(/^\s*import\s.*from\s*["']([^"']*)$/);
  if (importMatch) {
    const from = context.pos - importMatch[1].length;
    const modulePrefix = importMatch[1];
    const moduleNames = await ((_a = worker.getModuleNames) == null ? void 0 : _a.call(worker)) ?? [];
    const options2 = moduleNames.filter((name) => name.startsWith(modulePrefix)).map((name) => ({
      label: name,
      type: "import",
      apply: name
    }));
    return {
      from,
      options: options2,
      filter: false
    };
  }
  const word = matchText(context);
  const blockComment = context.matchBefore(/\/\*?\*?/);
  if (blockComment) {
    return { from: blockComment == null ? void 0 : blockComment.from, options: [snippets.blockCommentSnippet] };
  }
  if (!word) return null;
  const completionResult = await worker.getCompletionsAtPos(context.pos);
  if (!completionResult || context.aborted) return null;
  const { result, isGlobal } = completionResult;
  let options = [...result.options];
  if (isGlobal) {
    options = options.concat(snippets.snippets);
  }
  return {
    from: word ? START_CHARACTERS.includes(word.text) ? word.to : word.from : context.pos,
    filter: false,
    getMatch(completion) {
      const lcp = index.longestCommonPrefix(completion.label, word.text);
      return [0, lcp.length];
    },
    options: options.filter(
      (option) => word.text === "" || START_CHARACTERS.includes(word.text) || index.prefixMatch(
        option.label.replace(START_CHARACTERS_REGEX, ""),
        word.text.replace(START_CHARACTERS_REGEX, "")
      )
    ).map((completion) => {
      if (completion.label.endsWith("()")) {
        completion.apply = (view, _, from, to) => {
          const cursorPosition = from + completion.label.length - 1;
          view.dispatch({
            ...index$1.insertCompletionText(view.state, completion.label, from, to),
            annotations: index$1.pickedCompletion.of(completion),
            selection: { anchor: cursorPosition, head: cursorPosition }
          });
        };
      }
      return completion;
    })
  };
};
exports.matchText = matchText;
exports.typescriptCompletionSource = typescriptCompletionSource;
//# sourceMappingURL=completions.cjs.map
