import { aggregateAcrossTeams, formatAggregationResult, loopGet, } from "../lib/loop-api.js"; // ─── Loop API V2: /team/{agentId}/availability/{searchDate} ──── // Returns array of: { agentId, start, end } // Times are ISO datetime strings (e.g. "2026-04-10T06:00:00") // ──────────────────────────────────────────────────────────────── interface LoopTimeRange { agentId?: string; start?: string; end?: string; [key: string]: unknown; } export async function teamAvailability(params: { accountId: string; agentId: string; searchDate: string; teamName?: string; }): Promise { const { accountId, agentId, searchDate, teamName } = params; const result = await aggregateAcrossTeams( accountId, "team", "loop-team-availability", async (apiKey, team) => { const path = `/team/${encodeURIComponent(agentId)}/availability/${encodeURIComponent(searchDate)}`; const data = await loopGet(apiKey, path, "loop-team-availability", team); return Array.isArray(data) ? data : []; }, { teamName } ); return formatAggregationResult( result, (slot) => { const start = slot.start ? formatTime(slot.start) : "?"; const end = slot.end ? formatTime(slot.end) : "?"; return `- ${start} — ${end}`; }, `availability slots for ${searchDate}` ); } function formatTime(iso: string): string { const timePart = iso.split("T")[1]; return timePart ? timePart.slice(0, 5) : iso; }