"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchQueryCategories = fetchQueryCategories;
const hubSearch_1 = require("../../../../search/hubSearch");
const isFullyQualifiedCategory_1 = require("./isFullyQualifiedCategory");
/**
 * Aggregates categories from a search query. Note that aggregation categories are all lowercased.
 *
 * @param query The query to search for categories.
 * @param requestOptions The request options to use for the search.
 * @returns a flat array of fully qualified category paths, all lowercased (e.g. ["/categories/category1", "/categories/category1/subcategory1", ...])
 */
async function fetchQueryCategories(query, requestOptions) {
    var _a;
    const searchOptions = {
        num: 1,
        include: [],
        requestOptions: requestOptions,
        aggLimit: 200,
        httpMethod: "POST",
        aggFields: ["categories"],
    };
    const response = await (0, hubSearch_1.hubSearch)(query, searchOptions);
    const categoriesAggregation = (_a = response.aggregations) === null || _a === void 0 ? void 0 : _a.find((agg) => agg.field === "categories");
    if (!categoriesAggregation) {
        throw new Error(`No categories aggregation found while calculating categories for query`);
    }
    return categoriesAggregation.values
        .map((v) => v.value)
        .filter((v) => (0, isFullyQualifiedCategory_1.isFullyQualifiedCategory)(v));
}
//# sourceMappingURL=fetchQueryCategories.js.map