
import { editor } from 'monaco-editor';

function createTrustedTypesPolicy(policyName, policyOptions) {
  const monacoEnvironment = globalThis.MonacoEnvironment;
  if (monacoEnvironment?.createTrustedTypesPolicy) {
    try {
      return monacoEnvironment.createTrustedTypesPolicy(policyName, policyOptions);
    } catch (err) {
      console.error(err);
      return void 0;
    }
  }
  try {
    return globalThis.trustedTypes?.createPolicy(policyName, policyOptions);
  } catch (err) {
    console.error(err);
    return void 0;
  }
}
let ttPolicy;
if (typeof self === "object" && self.constructor && self.constructor.name === "DedicatedWorkerGlobalScope" && globalThis.workerttPolicy !== void 0) {
  ttPolicy = globalThis.workerttPolicy;
} else {
  ttPolicy = createTrustedTypesPolicy("defaultWorkerFactory", {
    createScriptURL: (value) => value
  });
}
function getWorkerBootstrapUrl(workerScriptUrl, workerOptions) {
  const blob = new Blob([[
    `${workerOptions.type === "module" ? "await import" : "importScripts"}(${JSON.stringify(workerScriptUrl)});`,
    `globalThis.postMessage({ type: 'vscode-worker-ready' });`
  ].join("")], { type: "application/javascript" });
  return URL.createObjectURL(blob);
}
function getWorker(descriptor) {
  const label = descriptor.label;
  const monacoEnvironment = globalThis.MonacoEnvironment;
  if (monacoEnvironment) {
    if (typeof monacoEnvironment.getWorker === "function") {
      return monacoEnvironment.getWorker("workerMain.js", label);
    }
    if (typeof monacoEnvironment.getWorkerUrl === "function") {
      const workerUrl = monacoEnvironment.getWorkerUrl("workerMain.js", label);
      const workerOptions = { name: label, type: "module", ...monacoEnvironment.getWorkerOptions?.("workerMain.js", label) };
      return new Worker(
        getWorkerBootstrapUrl(ttPolicy ? ttPolicy.createScriptURL(workerUrl) : workerUrl, workerOptions),
        workerOptions
      );
    }
  }
  if (descriptor.createWorker) {
    return descriptor.createWorker();
  }
  throw new Error(
    `You must define a function MonacoEnvironment.getWorkerUrl or MonacoEnvironment.getWorker`
  );
}
function createWebWorker(opts) {
  const worker = Promise.resolve(
    getWorker({
      label: opts.label ?? "monaco-editor-worker",
      moduleId: opts.moduleId,
      createWorker: opts.createWorker
    })
  ).then((w) => {
    w.postMessage("ignore");
    w.postMessage(opts.createData);
    return w;
  });
  return editor.createWebWorker({
    worker,
    host: opts.host,
    keepIdleModels: opts.keepIdleModels
  });
}

export { createWebWorker };
