import { InferenceWorkerHost } from "./inference-worker";
import type { RuntimeMessage } from "../shared/types";

const modelBaseUrl = chrome.runtime.getURL("models/toxicity/");
const inferenceWorker = new InferenceWorkerHost(() => new Worker(chrome.runtime.getURL("inference.js"), { type: "module" }), modelBaseUrl);

chrome.runtime.onMessage.addListener((message: RuntimeMessage, _sender, sendResponse) => {
  if (message.type !== "OFFSCREEN_INFER") return false;
  inferenceWorker.run(message.requests)
    .then((results) => sendResponse({ results }))
    .catch((error: unknown) => sendResponse({ error: error instanceof Error ? error.message : String(error) }));
  return true;
});
