import { tableFromIPC } from 'apache-arrow';

/**
 * Parses an Apache Arrow IPC stream from a Response body.
 */
export async function parseArrowStream(response: Response) {
  // Use arrayBuffer() directly - more reliable than streaming
  const buffer = await response.arrayBuffer();
  const combined = new Uint8Array(buffer);
  
  const table = tableFromIPC(combined);
  
  // Convert to array of objects for easier TanStack Table consumption
  // Note: For extreme performance with 1M+ rows, we'd avoid this, 
  // but for 50k it's acceptable and easier to work with.
  // Also convert BigInt to Number to avoid JSON serialization issues
  return table.toArray().map((row) => {
    const obj = row.toJSON();
    for (const key in obj) {
      if (typeof obj[key] === 'bigint') {
        obj[key] = Number(obj[key]);
      }
    }
    return obj;
  });
}
