/* eslint-disable @typescript-eslint/no-explicit-any */
// content.ts
import { productApi } from "./api/product-api.service";
import { delay, delayRD } from "./features/app";
import { facebookService } from "./services/facebook.service";

const port = chrome.runtime.connect();

// Listent event to get payload publist
port.onMessage.addListener(async (message) => {
  if (message.type === "PUBLIST_EVENT") {
    const data = message.payload as IItem;

    if (!data) return;

    console.log("[PUBLIST_EVENT] Received new product event:", data);

    try {
      await delayRD(500, 600);
      await facebookService.handlePublist(data);
    } catch (error) {
      await productApi.finistPublist(data, {
        error: (error as { message: string }).message,
        published: false,
      });
    } finally {
      await productApi.finistPublist(data, { published: true });

      await delay(5000);

      await facebookService.closeTab(data);
    }
  }
});

// Listent event to get payload edit
port.onMessage.addListener(async (message) => {
  if (message.type === "EDIT_EVENT") {
    const { data, prev } = message.payload as { prev: IItem; data: IItem };

    if (!data) return;

    console.log("[PUBLIST_EVENT] Received new product event:", data);

    try {
      await facebookService.handleUpdate(data);
    } catch (error) {
      await productApi.updatePublist(data, {
        error: (error as { message: string }).message,
        published: true,
        publist_id: prev.publist_id,
      });
    } finally {
      await delay(3000);

      await productApi.updatePublist(data, {
        published: true,
        message: `Edited product ${data.title}`,
        publist_id: prev.publist_id,
      });

      await facebookService.closeTab(data);
    }
  }
});

// Listent event to delete
chrome.runtime.onMessage.addListener(async (message) => {
  if (message.type === "DELETE_STREAM_DATA") {
    const data = message.payload as IItem;

    if (!data) return;

    console.log("[DELETE_STREAM_DATA] Received new product event:", data);

    try {
      await facebookService.handleDelete(message.payload);
    } catch (error) {
      await productApi.finistPublist(data, {
        error: (error as { message: string }).message,
        published: false,
      });
    }
  }
});

// Listent event to get publist id
chrome.runtime.onMessage.addListener(async (message) => {
  if (message.type === "GET_PUBLIST_ID") {
    console.log({ message });
    const {
      data: { prev },
    } = message;

    const publist_id = await facebookService.handleGetPublistID(prev);

    // Gửi message đến background/content script thông báo là get đã xong
    chrome.runtime.sendMessage({ type: "GET_PUBLIST_ID_DONE", publist_id });
  }
  return true; // Để cho phép async sendResponse
});

async function init() {
  // const { data } = await axios.get("products/53");

  // if (!data.data) return;

  // const item = data.data as IItem;

  // await facebookService.handleUpdate(item);

  await facebookService.syncListing();
}

init();
