// background.ts
import { BackgroundService } from "@/services/background.service";
import PQueue from "p-queue";

// Hàng đợi để xử lý tuần tự
const queue = new PQueue({ concurrency: 1 });

const service = new BackgroundService();

// Forward hoặc xử lý logic
service.msgService.on("content-to-background-actions", (payload) => {
  console.log("Background nhận từ popup:", payload);
  // service.msgService.send("content", "popup-to-content", payload);

  switch (payload?.type) {
    case "publist": {
      queue.add(() => service.publishJob(payload.data, 60000));
      break;
    }
    case "unlist": {
      queue.add(() => service.unlistJob(payload.data, 60000));
      break;
    }
    case "re-publist": {
      queue.add(() => service.rePublistJob(payload.data, 60000));
      break;
    }
  }
});

service.msgService.on("content-to-popup", (payload) => {
  console.log("Background nhận từ content:", payload);
  service.msgService.send("popup", "content-to-popup", payload);
});

// Listent to handle create blob image urls
service.msgService.on<string[]>(
  "content-to-background-create-blod-urls",
  (payload) => service.createBlobUrls(payload)
);
