import { batch } from "../../utils/batch";
import { getPredicateValues } from "../getPredicateValues";
import { portalToSearchResult } from "../../org/fetch";
import { fetchOrg } from "../../org/fetch-org";
import { failSafe } from "../../utils/fail-safe";
/**
 * Since the portal api does not actually have a search endpoint for orgs,
 * this function simply extracts the id predicate from the query and
 * fetches the orgs that match that ids.
 * @param query
 * @param options
 */
export async function portalFetchOrgs(query, options) {
    // get the id predicate from the query
    const ids = getPredicateValues("id", query, ["any"]);
    if (!ids.length) {
        throw new Error("Organization query must contain an id predicate.");
    }
    const failSafeFetchOrg = failSafe(fetchOrg, null);
    const fetchOrgFn = async (id) => {
        // fetch the org
        return failSafeFetchOrg(id, options.requestOptions);
    };
    // fetch the orgs and remove any nulls from the failsafe
    const results = (await batch(ids, fetchOrgFn, 8)).filter((org) => org !== null);
    // convert to a search result
    return {
        total: results.length,
        hasNext: false,
        next: null,
        results: results.map((org) => portalToSearchResult(org)),
    };
}
//# sourceMappingURL=portalFetchOrgs.js.map