import * as monaco from "monaco-editor-core";
import { OpenQasmWorker } from "./openqasmWorker";
import { LANGUAGE_ID } from '../constants';

export class WorkerManager {

  private worker: monaco.editor.MonacoWebWorker<OpenQasmWorker> | null;
  private workerClientProxy: Promise<OpenQasmWorker> | undefined;

  constructor() {
    this.worker = null;
  }

  private getClientProxy() {
    if (!this.workerClientProxy) {
      this.worker = monaco.editor.createWebWorker<OpenQasmWorker>({
        moduleId: LANGUAGE_ID,
        label: LANGUAGE_ID,
        createData: {
          languageId: LANGUAGE_ID,
        }
      });
      this.workerClientProxy = this.worker.getProxy();
    }
    return this.workerClientProxy;
  }

  async getLanguageServiceWorker(...resources: monaco.Uri[]) {

    const client = await this.getClientProxy();
    if (this.worker) {
      await this.worker.withSyncedResources(resources);
    }
    return client;

  }

}
