"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.portalFetchOrgs = portalFetchOrgs;
const batch_1 = require("../../utils/batch");
const getPredicateValues_1 = require("../getPredicateValues");
const fetch_1 = require("../../org/fetch");
const fetch_org_1 = require("../../org/fetch-org");
const fail_safe_1 = require("../../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
 */
async function portalFetchOrgs(query, options) {
    // get the id predicate from the query
    const ids = (0, getPredicateValues_1.getPredicateValues)("id", query, ["any"]);
    if (!ids.length) {
        throw new Error("Organization query must contain an id predicate.");
    }
    const failSafeFetchOrg = (0, fail_safe_1.failSafe)(fetch_org_1.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 (0, batch_1.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) => (0, fetch_1.portalToSearchResult)(org)),
    };
}
//# sourceMappingURL=portalFetchOrgs.js.map