// src/lib/db/chunk.ts — split rows for bulk INSERTs.
// Postgres caps a single statement at ~65 535 bind parameters; with ~14
// params per row a flat INSERT fails around 4 600 rows. Callers loop over
// chunks inside one db.transaction so the write stays atomic.
// Pure helper (no server-only) — also imported by services/analytics.

/** Split `rows` into consecutive slices of at most `size` elements. */
export function chunk<T>(rows: readonly T[], size: number): T[][] {
  const out: T[][] = [];
  for (let i = 0; i < rows.length; i += size) {
    out.push(rows.slice(i, i + size));
  }
  return out;
}
