import { createGetHappeningTreeContext, getHappeningTree } from "./get-happening-tree.js";
import { listHappenings } from "./list-happenings.js";
import { getAttendee } from "../attendee/index.js";
import { ok } from "../../is.js";
export async function listHappeningTrees(options) {
    return listHappeningTreesWithContext(await getListHappeningTreesContext(options));
}
export async function listHappeningTreesWithContext(context) {
    const nonUniqueTrees = await Promise.all(map(context.store.values(), (value) => {
        const happeningId = value[context.idKey];
        ok(typeof happeningId === "string");
        return getHappeningTree(happeningId, context);
    }));
    // These trees without parents will contain all trees in nonUniqueTrees
    return nonUniqueTrees.filter(tree => !tree.parent);
}
function map(iterable, fn) {
    const returnValues = [];
    for (const value of iterable) {
        returnValues.push(fn(value));
    }
    return returnValues;
}
async function getListHappeningTreesContext(options) {
    const happenings = await listHappenings(options);
    const attendeeIds = [...new Set(happenings.flatMap(happening => happening.attendees))];
    const attendees = await Promise.all(attendeeIds.map(getAttendee));
    return createGetHappeningTreeContext({ happenings, attendees });
}
//# sourceMappingURL=list-happening-trees.js.map