import crypto from 'crypto-js';
import { LazadaOrderPdfPrintJob } from '@/models/orderPdf';
import { getPdfFromUrl } from '@/api/utils/get';
import { saveLogPrintJob } from './service';

const PLATFORM = 'lazada';
const REGEX_EXTRACT_ID = {
  trackingCodes: /\bLMP[A-Z0-9]{10,20}\b/g,
  orderCodes: /\b\d{11,15}\b/g,
};

/**
 * @param {string} pdfUrl
 * @param {string} shopId
 * @param {string} shopName
 * @returns {Promise<OrderPdfBucket>}
 */
export const lazadaSaveLogPrintJob = async (pdfUrl, shopId, shopName) => {
  const printId = crypto.SHA256(pdfUrl).toString();

  return saveLogPrintJob(
    printId,
    shopId,
    shopName,
    PLATFORM,
    LazadaOrderPdfPrintJob,
    () => getPdfFromUrl(pdfUrl),
    REGEX_EXTRACT_ID,
  );
};
