"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.hubSearchPosts = hubSearchPosts;
const postToSearchResult_1 = require("./hubDiscussionsHelpers/postToSearchResult");
const processPostFilters_1 = require("./hubDiscussionsHelpers/processPostFilters");
const processPostOptions_1 = require("./hubDiscussionsHelpers/processPostOptions");
const processRelations_1 = require("./hubDiscussionsHelpers/processRelations");
const posts_1 = require("../../discussions/api/posts/posts");
/**
 * @private
 * Executes a Discussions API (v2) post search and resolves an IHubSearchResponse<IHubSearchResult>.
 * Currently supported filters include (filter predicate key => accepted values):
 *   - access: 'public' | 'private' | 'org' | Array<'public' | 'private' | 'org'>
 *   - channel: string | string[]
 *   - owner: string
 *   - discussion: string
 *   - editor: string
 *   - status: 'pending' | 'approved' | 'rejected' | 'deleted' | 'hidden' | 'blocked' | Array<...>
 *   - term: string
 *   - title: string
 *   - body: string
 *   - bbox: string
 *   - parentId: string | string[]
 *   - groups: string | string[]
 *   - postType: 'text' | 'announcement' | 'poll' | 'question'
 *   - created: IDateRange<string | number> (mapped to createdAfter / createdBefore)
 *   - modified: IDateRange<string | number> (mapped to updatedAfter / updatedBefore)
 *
 * Notes / constraints:
 *   - For single-value filters where an array is provided (e.g., owner, editor, discussion, term, title, body, bbox), only the first value is used.
 *   - title & body are ignored if term is provied, as term searches against both title and body
 *   - Date range predicates use the shape: { from: string|number; to: string|number }.
 *   - Unsupported filters are ignored silently.
 *
 * INCLUDE SUPPORT (options.include tokens mapped to post relations - bare tokens only)
 *   channel     -> PostRelation.CHANNEL
 *   parent      -> PostRelation.PARENT
 *   replies     -> PostRelation.REPLIES (returns replies collection or paging info)
 *   replyCount  -> PostRelation.REPLY_COUNT (numeric aggregate)
 *   reactions   -> PostRelation.REACTIONS
 *   channelAcl  -> PostRelation.CHANNEL_ACL
 *   Unknown tokens are ignored; no namespaced forms (e.g. `post:channel`) are supported.
 *
 * Currently supported sort fields include (pass via `options.sortField`):
 *   - body
 *   - channelId
 *   - created  (maps to createdAt)
 *   - modified (maps to updatedAt)
 *   - owner    (maps to creator)
 *   - discussion
 *   - editor
 *   - id
 *   - parentId
 *   - status
 *   - title
 * Sort order: 'ASC' | 'DESC' (via `options.sortOrder`).
 * Pagination options: `options.num` (page size), `options.start` (1-based offset handled by API).
 *
 * @param query An IQuery object (must include `filters` and `targetEntity: 'post'` upstream)
 * @param options An IHubSearchOptions object
 * @returns a promise that resolves an IHubSearchResponse<IHubSearchResult>
 */
async function hubSearchPosts(query, options) {
    const processedFilters = (0, processPostFilters_1.processPostFilters)(query.filters);
    const processedOptions = (0, processPostOptions_1.processPostOptions)(options);
    const processedRelations = (0, processRelations_1.processRelations)(options.include);
    const data = Object.assign(Object.assign(Object.assign({}, processedFilters), processedOptions), { relations: processedRelations });
    const { items, nextStart, total } = await (0, posts_1.searchPostsV2)(Object.assign(Object.assign({}, options.requestOptions), { 
        // Passing `hubApiKey` as `token` to `searchPostsV2`. The token is defined as
        // a prop in `IDiscussionsRequestOptions`, which `ISearchPostsParams` extends
        token: options.requestOptions.hubApiKey, data }));
    const results = await Promise.all(items.map((post) => (0, postToSearchResult_1.postToSearchResult)(post)));
    const hasNext = nextStart > -1;
    return {
        total,
        results,
        hasNext,
        next: () => {
            if (!hasNext) {
                throw new Error("No more hub posts for the given query and options");
            }
            return hubSearchPosts(query, Object.assign(Object.assign({}, options), { start: nextStart }));
        },
    };
}
//# sourceMappingURL=hubSearchPosts.js.map