import { callGraph } from "../lib/graph-client.js";
import { log } from "../lib/log.js";
const FIELDS = "id,subject,start,end,location,attendees,isAllDay,organizer";
export async function runCalendarList(config, args) {
    const rangeDays = clamp(args.rangeDays ?? 7, 1, 365);
    const top = clamp(args.top ?? 100, 1, 500);
    const startMs = Date.now();
    const startISO = new Date().toISOString();
    const endISO = new Date(Date.now() + rangeDays * 24 * 60 * 60 * 1000).toISOString();
    const result = await callGraph(config, async (client) => {
        return (await client
            .api("/me/calendarView")
            .query({ startDateTime: startISO, endDateTime: endISO })
            .top(top)
            .orderby("start/dateTime ASC")
            .select(FIELDS)
            .get());
    }, { tool: "calendar-list" });
    const events = result.value.map(shapeEvent);
    log({
        event: "calendar-list",
        account: config.accountId,
        rangeDays,
        count: events.length,
        elapsedMs: Date.now() - startMs,
    });
    return events;
}
export function shapeEvent(ev) {
    return {
        id: ev.id,
        subject: ev.subject ?? null,
        start: ev.start,
        end: ev.end,
        location: ev.location?.displayName ?? null,
        attendees: ev.attendees?.map((a) => ({
            email: a.emailAddress.address,
            name: a.emailAddress.name ?? null,
            response: a.status?.response ?? "none",
        })) ?? [],
        isAllDay: ev.isAllDay ?? false,
        organizer: ev.organizer?.emailAddress?.address ?? null,
    };
}
function clamp(input, lo, hi) {
    if (!Number.isFinite(input))
        return lo;
    if (input < lo)
        return lo;
    if (input > hi)
        return hi;
    return Math.floor(input);
}
//# sourceMappingURL=calendar-list.js.map