var c = Object.defineProperty;
var i = (o, e) => c(o, "name", { value: e, configurable: !0 });
import { LanguageServerService as m } from "./api/proto/exa/language_server_pb/language_server_connect.js";
import { createPromiseClient as p } from "../../../../../@connectrpc_connect@1.6.1_@bufbuild_protobuf@1.10.0/node_modules/@connectrpc/connect/dist/esm/promise-client.js";
import { createConnectTransport as l } from "../../../../../@connectrpc_connect-web@1.6.1_@bufbuild_protobuf@1.10.0_@connectrpc_connect@1.6.1/node_modules/@connectrpc/connect-web/dist/esm/connect-transport.js";
const d = l({
  baseUrl: "https://web-backend.codeium.com",
  useBinaryFormat: !0
}), g = p(m, d);
let s;
try {
  s = crypto.randomUUID();
} catch {
  let o = function() {
    return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, (e) => (+e ^ (crypto.getRandomValues(new Uint8Array(1))[0] ?? 0) & 15 >> +e / 4).toString(16));
  };
  i(o, "uuidv4"), s = o();
}
async function S({ text: o, cursorOffset: e, config: t, otherDocuments: n }) {
  return await g.getCompletions({
    metadata: {
      ideName: "web",
      ideVersion: "0.0.5",
      extensionName: "@valtown/codemirror-codeium",
      extensionVersion: "1.0.0",
      apiKey: t.apiKey,
      sessionId: s,
      authSource: t.authSource
    },
    document: {
      text: o,
      cursorOffset: BigInt(e),
      language: t.language,
      // TODO: not sure why we have both language and
      // editorlanguage
      // The types don't like this here, but it works.
      editorLanguage: "typescript",
      lineEnding: `
`
    },
    editorOptions: {
      tabSize: 2n,
      insertSpaces: !0
    },
    otherDocuments: n,
    multilineConfig: void 0
  }, {
    // signal,
    headers: {
      Authorization: `Basic ${t.apiKey}-${s}`
    }
  });
}
i(S, "getCodeiumCompletions");
function x(o) {
  return o.completionItems.map((e) => {
    let t = 0;
    return e.completionParts.filter((n) => n.type !== 3).map((n) => {
      const r = Number(n.offset), a = n.type === 2 ? `
${n.text}` : n.text, u = {
        absoluteStartPos: t + r,
        absoluteEndPos: t + r + a.length,
        from: r,
        to: r,
        insert: a
      };
      return t += a.length, u;
    });
  });
}
i(x, "completionsToChangeSpec");
export {
  x as completionsToChangeSpec,
  S as getCodeiumCompletions
};
