import { DrawTableError, DrawTableOptions, CellContent } from ".."; // Function to handle table overflow export function handleOverflow( rows: CellContent[][], error: DrawTableError, headers: CellContent[], options?: DrawTableOptions ): { remainingRows: CellContent[][], newOverflowRows?: CellContent[][] } { try { // Validate error dimensions before proceeding if (!error.dimensions || !error.dimensions.height || !error.dimensions.endY) { throw new Error("Incomplete dimensions in DrawTableError."); } const numRowsToRemove = calculateNumRowsToRemove(error, error.dimensions.height, error.dimensions.endY); if (numRowsToRemove <= 0) { return { remainingRows: rows }; // No overflow handling needed } const overflowRows = rows.splice(-numRowsToRemove); return { remainingRows: rows, newOverflowRows: overflowRows }; } catch (e) { console.error("Error handling table overflow:", e); return { remainingRows: rows }; } } // Function to calculate the number of rows to remove based on the overflow error function calculateNumRowsToRemove(error: DrawTableError, pageHeight: number, currentY: number): number { try { const availableSpace = pageHeight - currentY -50; let cumulativeHeight = 0; let numRows = 0; for (let height of error.rowHeights || []) { cumulativeHeight += height; if (cumulativeHeight > availableSpace) { break; } numRows++; } return (error.rowHeights?.length || 0) - numRows; } catch (e) { console.error("Error calculating number of rows to remove:", e); return 0; } }