import { copilotPlugin } from '../../../../../node_modules/.pnpm/@valtown_codemirror-codeium@1.1.1_@codemirror_autocomplete@6.17.0_@codemirror_language@6.10.2_kzathhoyoklffgxuk2fuimw3gq/node_modules/@valtown/codemirror-codeium/dist/plugin.js';
import { keymap } from '../../../../../node_modules/.pnpm/@codemirror_view@6.28.6/node_modules/@codemirror/view/dist/index.js';
import { Language } from '../../../../../node_modules/.pnpm/@valtown_codemirror-codeium@1.1.1_@codemirror_autocomplete@6.17.0_@codemirror_language@6.10.2_kzathhoyoklffgxuk2fuimw3gq/node_modules/@valtown/codemirror-codeium/dist/api/proto/exa/codeium_common_pb/codeium_common_pb.js';
import { codeiumOtherDocumentsConfig } from '../../../../../node_modules/.pnpm/@valtown_codemirror-codeium@1.1.1_@codemirror_autocomplete@6.17.0_@codemirror_language@6.10.2_kzathhoyoklffgxuk2fuimw3gq/node_modules/@valtown/codemirror-codeium/dist/config.js';
import { startCompletion } from '../../../../../node_modules/.pnpm/@valtown_codemirror-codeium@1.1.1_@codemirror_autocomplete@6.17.0_@codemirror_language@6.10.2_kzathhoyoklffgxuk2fuimw3gq/node_modules/@valtown/codemirror-codeium/dist/commands.js';

const urls = [
  // "https://esm.town/v/dinavinter/htmx_layout",
  // "https://esm.town/v/dinavinter/cxai/yjs/store",
  // "https://esm.town/v/dinavinter/cxai/yjs/faker",
  // "https://esm.town/v/dinavinter/cxai/yjs/job",
  // "https://esm.town/v/dinavinter/cxai/yjs/last",  
  // "https://esm.town/v/dinavinter/cxai/yjs/react-hooks",
  // "https://esm.town/v/dinavinter/dcom/spotlight",
];
const otherDocuments = await Promise.all(urls.map(async (url) => ({
  absolutePath: url,
  text: await fetch(url).then((res) => res.text()),
  language: Language.JAVASCRIPT,
  editorLanguage: "typescript"
})));
function codeiumCopilot() {
  return [
    codeiumOtherDocumentsConfig.of({
      override: () => otherDocuments
    }),
    copilotPlugin({
      apiKey: "d49954eb-cfba-4992-980f-d8fb37f0e942",
      shouldComplete(context) {
        if (context.tokenBefore(["String"])) {
          return true;
        }
        const match = context.matchBefore(/(@(?:\w*))(?:[./](\w*))?/);
        return !match;
      }
    }),
    keymap.of([
      {
        key: "Cmd-k",
        run: startCompletion
      }
    ])
  ];
}
const copilotStyle = `
.cm-ghostText,
.cm-ghostText * {
  opacity: 0.6;
  filter: grayscale(20%);
  cursor: pointer;
}

.cm-ghostText:hover {
  background: #eee;
}
`;

export { codeiumCopilot, copilotStyle };
