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