import { parseRequest } from '@background/bridge/listener/utils';
import { pushLazadaPrintLogJob } from '@/service/print/log';
import { LazadaShopIdentify } from '@background/store/shop';
import { getUserInfo } from '@background/service/index';
import { storageSaveHistoryPrint } from '@/plugins/storage/items';

/**
 * @param {BridgeListenerRequestPayload} payload
 * @returns {Promise<object>}
 */
export const onSellerPrintOrder = async (payload) => {
  const [{ isAuthed }, saveHistoryPrint] = await Promise.all([
    getUserInfo(),
    storageSaveHistoryPrint.getValue(),
  ]);

  if (!isAuthed || !saveHistoryPrint || !saveHistoryPrint.lazada) {
    return null;
  }

  const { query, url: printUrl } = parseRequest(payload);

  if (!query) {
    return null;
  }

  const shopId = LazadaShopIdentify.shopId;
  const shopName = LazadaShopIdentify.shopName;

  pushLazadaPrintLogJob(printUrl, shopId, shopName);

  return {
    printUrl,
  };
};
