import { channelToSearchResult, getChannelGroupIds, } from "../../../discussions/utils";
import { searchGroups } from "@esri/arcgis-rest-portal";
import { batch } from "../../../utils/batch";
import { splitArrayByLength } from "../../../utils/_array";
/**
 * @private
 * Convert an array of Discussions API channel search results into an
 * array of IHubSearchResult objects
 * @param channels An array of IChannel objects
 * @param options an IHubSearchOptions object
 * @returns a promise that resolves an array of IHubSearchResult objects
 */
export async function channelResultsToSearchResults(channels, options) {
    var _a;
    let groupResults = [];
    if ((_a = options.include) === null || _a === void 0 ? void 0 : _a.includes("groups")) {
        // we can search for up to 100 channels at a time that can each
        // have up to 100 groups...
        const uniqueGroupIds = channels.reduce((acc, channel) => getChannelGroupIds(channel).reduce((memo, groupId) => memo.includes(groupId) ? memo : [...memo, groupId], acc), []);
        // conduct up to 5 concurrent searches that can request
        // up to 100 ids per search. if less than 101 ids exist,
        // it performs a single search
        const batchResults = await batch(splitArrayByLength(uniqueGroupIds, 100), (groupIds) => searchGroups(Object.assign({ q: groupIds.map((id) => `id:${id}`).join(" OR "), num: groupIds.length }, options.requestOptions)), 5);
        // reassemble the batch results into a single array
        groupResults = batchResults.reduce((acc, batchResult) => [...acc, ...batchResult.results], []);
    }
    return channels.map((channel) => {
        var _a;
        return channelToSearchResult(channel, ((_a = options.include) === null || _a === void 0 ? void 0 : _a.includes("groups"))
            ? getChannelGroupIds(channel).map((groupId) => groupResults.find(({ id }) => id === groupId) || null)
            : []);
    });
}
//# sourceMappingURL=channelResultsToSearchResults.js.map