import { aggregateAcrossTeams, formatAggregationResult, loopGet, } from "../lib/loop-api.js";
export async function viewingsList(params) {
    const { accountId, type, teamName, dateFrom, dateTo, limit } = params;
    const result = await aggregateAcrossTeams(accountId, "viewings", "loop-viewings-list", async (apiKey, team) => {
        const queryParts = [];
        if (type && type !== "both")
            queryParts.push(`type=${encodeURIComponent(type)}`);
        if (dateFrom)
            queryParts.push(`dateFrom=${encodeURIComponent(dateFrom)}`);
        if (dateTo)
            queryParts.push(`dateTo=${encodeURIComponent(dateTo)}`);
        const query = queryParts.length > 0 ? `?${queryParts.join("&")}` : "";
        const data = await loopGet(apiKey, `/viewings${query}`, "loop-viewings-list", team);
        return Array.isArray(data) ? data : [];
    }, { teamName, limitPerTeam: limit ?? 50, limitTotal: limit ?? 200 });
    return formatAggregationResult(result, (v) => {
        const addr = v.propertyAddress ?? "Unknown property";
        const when = [v.date, v.time].filter(Boolean).join(" ");
        const status = v.status ? ` [${v.status}]` : "";
        const attendee = v.attendeeName ? ` — ${v.attendeeName}` : "";
        const viewType = v.type ? ` (${v.type})` : "";
        return `- ${addr} — ${when || "No date"}${viewType}${attendee}${status}`;
    }, "viewings");
}
//# sourceMappingURL=viewings-list.js.map