/**
 * Supabase PostgREST passes `.in('col', ids)` as a URL parameter:
 *   ?col=in.(uuid1,uuid2,...)
 *
 * Each UUID is 36 chars + a comma = 37, so the URL grows ~37 chars per id.
 * Proxies (Cloudflare, the supabase ingress, etc.) commonly reject URLs
 * longer than 8–16 KB; we hit `TypeError: fetch failed` at the SDK layer
 * when that happens. Anything that builds an `.in(...)` filter from a
 * user-controlled list of IDs MUST chunk it.
 *
 * 150 UUIDs ≈ 5.6 KB worth of `.in()` payload — comfortably under typical
 * limits but big enough that a 600-row request only fans out to four
 * sequential queries.
 */
export const ID_CHUNK_SIZE = 150

/** Split an array into fixed-size chunks. Last chunk may be shorter. */
export function chunkIds<T>(ids: T[], size: number = ID_CHUNK_SIZE): T[][] {
  if (ids.length <= size) return [ids]
  const out: T[][] = []
  for (let i = 0; i < ids.length; i += size) {
    out.push(ids.slice(i, i + size))
  }
  return out
}
