import { hoverTooltip, EditorView } from "../_vendor/@codemirror/view/dist-index.js";
import { useRef, useCallback, useEffect } from "react";
import { wrap } from "../_vendor/comlink/dist-esm-comlink.js";
import { LanguageSupport } from "../_vendor/@codemirror/language/dist-index.js";
import { javascriptLanguage } from "../_vendor/@codemirror/lang-javascript/dist-index.js";
import { typescriptCompletionSource } from "../typescript/client/completions.js";
import { autocompletion } from "../_vendor/@codemirror/autocomplete/dist-index.js";
import { linter } from "../_vendor/@codemirror/lint/dist-index.js";
import WorkerWrapper from "../typescript/worker/typescript-worker.js";
import { typescriptWorkerFacet } from "../typescript/client/facet.js";
import { typescriptLintSource } from "../typescript/client/linter.js";
import { typescriptHoverTooltips } from "../typescript/client/hover-tooltip.js";
function useTypescript(props) {
  const { id } = props;
  const workerRef = useRef();
  const webWorkerRef = useRef();
  const createWorker = useCallback(async (initialContent) => {
    webWorkerRef.current = new WorkerWrapper();
    const { init } = wrap(webWorkerRef.current);
    const workerInstance = await init({
      id,
      content: initialContent
    });
    workerRef.current = workerInstance;
    return [
      typescriptWorkerFacet.of({ worker: workerInstance }),
      new LanguageSupport(javascriptLanguage, [
        javascriptLanguage.data.of({ autocomplete: typescriptCompletionSource })
      ]),
      autocompletion({ icons: false, aboveCursor: true }),
      linter(typescriptLintSource),
      hoverTooltip(typescriptHoverTooltips, {
        hideOnChange: true,
        hoverTime: 500
      }),
      EditorView.updateListener.of(async (update) => {
        if (update.docChanged) {
          void (workerInstance == null ? void 0 : workerInstance.updateFile(update.changes.toJSON()));
        }
      })
    ];
  }, [id]);
  useEffect(() => {
    return () => {
      if (webWorkerRef.current) {
        webWorkerRef.current.terminate();
      }
      workerRef.current = void 0;
    };
  }, []);
  return {
    createWorker
  };
}
export {
  useTypescript
};
//# sourceMappingURL=use-typescript.js.map
