"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.hubSearchEvents = hubSearchEvents;
const events_1 = require("../../events/api/events");
const orval_events_1 = require("../../events/api/orval/api/orval-events");
const eventToSearchResult_1 = require("./hubEventsHelpers/eventToSearchResult");
const processOptions_1 = require("./hubEventsHelpers/processOptions");
const processFilters_1 = require("./hubEventsHelpers/processFilters");
/**
 * @private
 * Searches for events against the Events 3 API using the given `query` and `options`.
 * Currently supported filters include:
 *   - access: 'public' | 'private' | 'org' | Array<'public' | 'org' | 'private'>;
 *   - canEdit: boolean
 *   - entityId: string | string[];
 *   - entityType: string | string[];
 *   - id: string | string[] | { not: string } | { not: string[] };
 *   - term: string;
 *   - categories: string | string[];
 *   - tags: string | string[];
 *   - group: string | string[] | { not: string } | { not: string[] };
 *   - attendanceType: 'virtual' | 'in_person' | Array<'virtual' | 'in_person'>;
 *   - occurrence: 'upcoming' | 'past' | 'inProgress'
 *   - owner: string | string[];
 *   - status: 'planned' | 'canceled' | 'removed' | Array<'planned' | 'canceled' | 'removed'>;
 *   - startDateBefore: string | number;
 *   - startDateAfter: string | number;
 *   - startDateRange: IDateRange<string | number>;
 *   - endDateRange: IDateRange<string | number>;
 *   - endDateBefore: string | number;
 *   - endDateAfter: string | number;
 *   - orgId: string;
 *   - modified: IDateRange<string | number>;
 * Currently supported sort fields include:
 *   - created
 *   - modified
 *   - title
 *   - startDate
 * @param query An IQuery object
 * @param options An IHubSearchOptions object
 * @returns a promise that resolves an IHubSearchResponse<IHubSearchResult> object
 */
async function hubSearchEvents(query, options) {
    const processedFilters = (0, processFilters_1.processFilters)(query.filters);
    const processedOptions = (0, processOptions_1.processOptions)(options);
    const data = Object.assign(Object.assign(Object.assign({}, processedFilters), processedOptions), { include: [orval_events_1.GetEventsInclude.creator, orval_events_1.GetEventsInclude.location] });
    const { items, nextStart, total } = await (0, events_1.searchEvents)(Object.assign(Object.assign({}, options.requestOptions), { data }));
    const results = await Promise.all(items.map((event) => (0, eventToSearchResult_1.eventToSearchResult)(event, options)));
    const hasNext = nextStart > -1;
    return {
        total,
        results,
        hasNext,
        next: () => {
            if (!hasNext) {
                throw new Error("No more hub events for the given query and options");
            }
            return hubSearchEvents(query, Object.assign(Object.assign({}, options), { start: nextStart }));
        },
    };
}
//# sourceMappingURL=hubSearchEvents.js.map