import './KlevuApiRawResponse-82e24d1e.js';
import { i as KlevuLastClickedProducts, g as KlevuTypeOfRecord } from './klevuFetch-ab326dc0.js';

var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
/**
 * Enable personlisation to the query
 *
 * @category Modifier
 * @param options
 * @returns
 */
function personalisation(options) {
    return {
        klevuModifierId: "personalisation",
        modifyAfter: (queries, klevuFunc) => __awaiter(this, void 0, void 0, function* () {
            var _a, _b;
            const copy = Array.from(queries);
            for (const q of queries) {
                if (!q.settings) {
                    q.settings = {};
                }
                q.settings.personalisation = {
                    enablePersonalisation: true,
                    fields: options === null || options === void 0 ? void 0 : options.fields,
                };
                let records = [];
                if ((options === null || options === void 0 ? void 0 : options.lastClickedProductIds) || (options === null || options === void 0 ? void 0 : options.lastClickedItemIds)) {
                    const ids = (_a = (options.lastClickedItemIds || options.lastClickedProductIds)) !== null && _a !== void 0 ? _a : [];
                    records = ids.map((pId) => ({
                        id: pId,
                    }));
                }
                else if (klevuFunc.klevuFunctionId === "categoryMerchandising" &&
                    ((_b = klevuFunc.params) === null || _b === void 0 ? void 0 : _b.category)) {
                    const { category } = klevuFunc.params;
                    records = KlevuLastClickedProducts.getCategoryPersonalisationIds(category).map((id) => ({ id }));
                }
                else {
                    records = KlevuLastClickedProducts.getLastClickedLatestsFirst(10, true).map((id) => ({ id }));
                }
                if (records.length > 0) {
                    q.settings.context = {
                        recentObjects: [
                            {
                                typeOfRecord: KlevuTypeOfRecord.Product,
                                records,
                            },
                        ],
                    };
                }
            }
            return copy;
        }),
    };
}

export { personalisation as p };

//# sourceMappingURL=personalisation-a27d067d.js.map