import { browserWebrequest } from '@/browser/api';
import { SHOPEE_URLS } from '@/constant/shopee';
import { TIKTOK_URLS } from '@/constant/tiktok';
import { LAZADA_URLS } from '@/constant/lazada';
import { shopeeBadWord } from './shopee/chat';
import {
  onFetchProductId, onFetchSearchProduct, onFetchClientAll,
} from './shopee/product';
import { onSendChatMessage } from './shopee/chat';
import { onSellerPrintOrder as onSellerPrintOrderShopee } from './shopee/print';
import { onSellerPrintOrder as onSellerPrintOrderTiktok } from './tiktok/print';
import { onSellerPrintOrder as onSellerPrintOrderLazada } from './lazada/print';

export const listenerService = async () => {
  browserWebrequest.onBeforeSendHeaders.addListener(
    onFetchProductId,
    { urls: [SHOPEE_URLS.PRODUCT_GET_PC] },
    ['requestHeaders'],
  );

  browserWebrequest.onBeforeSendHeaders.addListener(
    onFetchSearchProduct,
    { urls: [SHOPEE_URLS.SEARCH_SEARCH_ITEMS] },
    ['requestHeaders'],
  );

  browserWebrequest.onBeforeSendHeaders.addListener(
    onFetchClientAll,
    { urls: [SHOPEE_URLS.CLIENT_GLOBAL] },
    ['requestHeaders'],
  );

  browserWebrequest.onBeforeSendHeaders.addListener(
    onSellerPrintOrderShopee,
    { urls: [SHOPEE_URLS.SELLER_ORDER_PRINT_PDF] },
    ['requestHeaders'],
  );

  browserWebrequest.onBeforeSendHeaders.addListener(
    onSellerPrintOrderTiktok,
    { urls: [TIKTOK_URLS.SELLER_PRINT_PDF] },
    ['requestHeaders'],
  );

  browserWebrequest.onBeforeSendHeaders.addListener(
    onSellerPrintOrderLazada,
    { urls: [LAZADA_URLS.SELLER_PRINT_PDF] },
    ['requestHeaders'],
  );

  if (false) {
    await shopeeBadWord.fetchList();
    browserWebrequest.onBeforeRequest.addListener(
      onSendChatMessage,
      { urls: [SHOPEE_URLS.CHAT_WEBCHAT] },
      ['blocking', 'requestBody'],
    );
    setInterval(shopeeBadWord.fetchList, 5000);
  }
};
