import './KlevuApiRawResponse-82e24d1e.js';
import { f as KlevuLastClickedProducts, g as KlevuTypeOfRecord } from './klevuFetch-ffde321a.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;
            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) {
                    records = options.lastClickedProductIds.map((pId) => ({
                        id: pId,
                    }));
                }
                else if (klevuFunc.klevuFunctionId === "categoryMerchandising" &&
                    ((_a = klevuFunc.params) === null || _a === void 0 ? void 0 : _a.category)) {
                    const { category } = klevuFunc.params;
                    records = KlevuLastClickedProducts.getCategoryPersonalisationIds(category).map((id) => ({ id }));
                }
                else {
                    records = KlevuLastClickedProducts.getLastClickedLatestsFirst().map((id) => ({ id }));
                }
                if (records.length > 0) {
                    q.settings.context = {
                        recentObjects: [
                            {
                                typeOfRecord: KlevuTypeOfRecord.Product,
                                records,
                            },
                        ],
                    };
                }
            }
            return copy;
        }),
    };
}

export { personalisation as p };

//# sourceMappingURL=personalisation-6d5babaa.js.map