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

const PLATFORM = 'tiktok';

const REGEX_EXTRACT_ID = {
  trackingCodes: /\b\d{12}\b/g,
  orderCodes: /\b\d{18}\b/g,
};

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

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