import { callGraph } from "../lib/graph-client.js";
import { log } from "../lib/log.js";
const FIELDS = "id,displayName,emailAddresses,businessPhones,homePhones,mobilePhone,jobTitle,companyName";
export async function runContactsList(config, args) {
    const top = clamp(args.top ?? 50, 1, 250);
    const startMs = Date.now();
    const result = await callGraph(config, async (client) => {
        return (await client
            .api("/me/contacts")
            .top(top)
            .select(FIELDS)
            .get());
    }, { tool: "contacts-list" });
    const items = result.value.map((c) => ({
        id: c.id,
        displayName: c.displayName ?? null,
        emailAddresses: (c.emailAddresses ?? [])
            .map((e) => e.address)
            .filter((a) => typeof a === "string"),
        phones: [
            ...(c.businessPhones ?? []),
            ...(c.homePhones ?? []),
            ...(c.mobilePhone ? [c.mobilePhone] : []),
        ],
        jobTitle: c.jobTitle ?? null,
        companyName: c.companyName ?? null,
    }));
    log({
        event: "contacts-list",
        account: config.accountId,
        count: items.length,
        elapsedMs: Date.now() - startMs,
    });
    return items;
}
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=contacts-list.js.map