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 { platformSetting } from '@/plugins/setting';
/**
 * @param {BridgeListenerRequestPayload} payload
 * @returns {Promise<object>}
 */
export const onSellerPrintOrder = async (payload) => {
  const [{ isAuthed }, saveHistoryPrint] = await Promise.all([
    getUserInfo(),
    platformSetting.saveHistoryPrint.getSync(),
  ]);

  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,
  };
};
