import { parseRequest } from '@background/bridge/listener/utils';
import { pushTiktokPrintLogJob } from '@/service/print/log';
import { TiktokShopIdentify } 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.tiktok) {
    return null;
  }

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

  if (!query.sign) {
    return null;
  }

  const shopId = TiktokShopIdentify.shopId;
  const shopName = TiktokShopIdentify.shopName;

  pushTiktokPrintLogJob(printUrl, shopId, shopName);

  return {
    printUrl,
  };
};
