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