import getPocketBase from "@/lib/pocketbase"; export type AiCounts = Record; // The route rejects more than 500 ids per call. const MAX_IDS_PER_REQUEST = 500; async function fetchChunk(ids: string[], token: string): Promise { const resp = await fetch( `/api/v1/quotes/assistant/counts?ids=${encodeURIComponent(ids.join(","))}`, { headers: { Authorization: `Bearer ${token}` }, cache: "no-store" }, ); if (!resp.ok) return {}; const body: unknown = await resp.json(); if (!body || typeof body !== "object" || Array.isArray(body)) return {}; return body as AiCounts; } /** * Per-quote "AI lines awaiting review" counts for the quotes board badge. * Best-effort: any failure yields `{}` so the badge simply stays hidden and * the board is never affected. */ export async function fetchAiCounts(ids: string[]): Promise { if (ids.length === 0) return {}; try { const token = getPocketBase().authStore.token; if (!token) return {}; const chunks: string[][] = []; for (let i = 0; i < ids.length; i += MAX_IDS_PER_REQUEST) { chunks.push(ids.slice(i, i + MAX_IDS_PER_REQUEST)); } const results = await Promise.all(chunks.map((c) => fetchChunk(c, token))); return Object.assign({}, ...results) as AiCounts; } catch { return {}; } }