"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.hubSearchChannels = void 0;
const HubError_1 = require("../../HubError");
const processChannelFilters_1 = require("./hubDiscussionsHelpers/processChannelFilters");
const processChannelOptions_1 = require("./hubDiscussionsHelpers/processChannelOptions");
const channelResultsToSearchResults_1 = require("./hubDiscussionsHelpers/channelResultsToSearchResults");
const channels_1 = require("../../discussions/api/channels/channels");
/**
 * @private
 * Executes a Discussions API channel search and resolves an IHubSearchResponse<IHubSearchResult> for the channel results
 * Currently supported filters include:
 *   - term: string;
 *   - group: string | string[];
 *   - access: 'public' | 'private' | 'org' | Array<'public' | 'org' | 'private'>
 *   - id: string | string[] | { not: string } | { not: string[] };
 *   - orgId: string;
 *   - hasUserPosts: boolean;
 *   - discussion: string;
 *   - owner: string;
 *   - createdDateRange: IDateRange<string | number>;
 *   - updatedDateRange: IDateRange<string | number>;
 *   - role: 'read' | 'write' | 'readWrite' | 'moderate' | 'manage' | 'owner' | Array<'read' | 'write' | 'readWrite' | 'moderate' | 'manage' | 'owner'>
 * Currently supported sort fields include:
 *   - created
 *   - modified
 *   - owner
 *   - lastActivity
 * @param query an IQuery object
 * @param options an IHubSearchOptions object
 * @returns a promise that resolves an IHubSearchResponse<IHubSearchResult> object
 */
const hubSearchChannels = async (query, searchOptions) => {
    if (!searchOptions.requestOptions) {
        throw new HubError_1.default("hubSearchChannels", "options.requestOptions is required");
    }
    const filters = (0, processChannelFilters_1.processChannelFilters)(query.filters);
    const options = (0, processChannelOptions_1.processChannelOptions)(searchOptions);
    const { total, nextStart, items } = await (0, channels_1.searchChannelsV2)(Object.assign({ data: Object.assign(Object.assign({}, filters), options) }, searchOptions.requestOptions));
    const results = await (0, channelResultsToSearchResults_1.channelResultsToSearchResults)(items, searchOptions);
    return {
        total,
        results,
        hasNext: nextStart > -1,
        next: () => (0, exports.hubSearchChannels)(query, Object.assign(Object.assign({}, searchOptions), { start: nextStart })),
    };
};
exports.hubSearchChannels = hubSearchChannels;
//# sourceMappingURL=hubSearchChannels.js.map