import { c as KlevuEvents, e as extractActiveFilters, K as KlevuTypeOfRequest } from './klevuFetch-ffde321a.js';
import './KlevuDomEvents-a1ecbf7c.js';
import './KlevuApiRawResponse-82e24d1e.js';

/**
 * This modifier should be used with merchandising query. It sends
 * automatically correct event data to Klevu
 *
 * @category Modifier
 * @param title Title of the category page viewed
 * @returns
 */
function sendMerchandisingViewEvent(title, override) {
    return {
        klevuModifierId: "sendMerchandisingViewEvent",
        onResult: (res, f) => {
            var _a, _b, _c, _d;
            if (!f.params) {
                return res;
            }
            const { id, abtest } = f.params;
            if (f.klevuFunctionId !== "categoryMerchandising" || !id) {
                return res;
            }
            const categoryFunction = (_a = f.queries) === null || _a === void 0 ? void 0 : _a.find((q) => { var _a, _b; return Boolean((_b = (_a = q.settings) === null || _a === void 0 ? void 0 : _a.query) === null || _b === void 0 ? void 0 : _b.categoryPath); });
            const category = (_d = (_c = (_b = categoryFunction === null || categoryFunction === void 0 ? void 0 : categoryFunction.settings) === null || _b === void 0 ? void 0 : _b.query) === null || _c === void 0 ? void 0 : _c.categoryPath) !== null && _d !== void 0 ? _d : "unknown";
            const queryResult = res.queriesById(id);
            if (!queryResult) {
                return res;
            }
            KlevuEvents.categoryMerchandisingView({
                categoryTitle: title,
                klevuCategory: category,
                products: queryResult.records,
                pageStartsFrom: queryResult.meta.offset,
                abTestId: abtest === null || abtest === void 0 ? void 0 : abtest.abTestId,
                abTestVariantId: abtest === null || abtest === void 0 ? void 0 : abtest.abTestVariantId,
                activeFilters: extractActiveFilters(queryResult),
                override,
            });
            return res;
        },
    };
}

const defaults = {
    id: "categoryMerchandising",
    searchTerm: "*",
    limit: 24,
};
/**
 * Fetch products for a category listing page.
 * Default result id is "categoryMerchandising".
 *
 * @category Query
 * @param category
 * @param options
 * @returns
 */
function categoryMerchandising(category, options, ...modifiers) {
    const params = Object.assign(Object.assign({}, defaults), options);
    const query = {
        id: params.id,
        typeOfRequest: KlevuTypeOfRequest.CategoryNavigation,
        settings: Object.assign({ query: {
                categoryPath: category,
                term: params.searchTerm,
            } }, params),
    };
    return {
        klevuFunctionId: "categoryMerchandising",
        queries: [query],
        modifiers,
        params: Object.assign(Object.assign({}, params), { category }),
    };
}

export { categoryMerchandising as c, sendMerchandisingViewEvent as s };

//# sourceMappingURL=categoryMerchandising-8950a4d7.js.map