import axios from 'axios'; // Use same origin in production (when deployed), localhost in development const API_BASE_URL = import.meta.env.VITE_API_URL || window.location.origin; /** * Process a single invoice image * @param {Blob} imageBlob - Image blob * @param {string} filename - Original filename * @param {boolean} enhanceImage - Whether to apply OpenCV enhancement * @param {string} reasoningMode - VLM reasoning mode: "simple" or "reason" * @returns {Promise} Processed result */ export async function processSingleInvoice(imageBlob, filename, enhanceImage = false, reasoningMode = "simple") { const formData = new FormData(); formData.append('file', imageBlob, filename); formData.append('enhance_image', enhanceImage); formData.append('reasoning_mode', reasoningMode); const response = await axios.post(`${API_BASE_URL}/process-invoice`, formData, { headers: { 'Content-Type': 'multipart/form-data', }, }); return response.data; } /** * Process multiple invoices in batch * @param {Array} images - Array of {blob, filename} objects * @param {Function} onProgress - Progress callback (index, result) * @returns {Promise} Array of results */ export async function processBatchInvoices(images, onProgress) { const results = []; for (let i = 0; i < images.length; i++) { try { const result = await processSingleInvoice(images[i].blob, images[i].filename); const resultWithMetadata = { ...result, filename: images[i].filename, originalFile: images[i].originalFile, pageNumber: images[i].pageNumber, index: i, success: true }; results.push(resultWithMetadata); if (onProgress) { onProgress(i, resultWithMetadata); } } catch (error) { const errorResult = { filename: images[i].filename, originalFile: images[i].originalFile, pageNumber: images[i].pageNumber, index: i, success: false, error: error.response?.data?.detail || error.message }; results.push(errorResult); if (onProgress) { onProgress(i, errorResult); } } } return results; }