"use strict";
var __awaiter = (this && this.__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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SearchMixin = void 0;
const base_core_sdk_1 = require("../mixins/base-core-sdk");
const subgraph_1 = require("./subgraph");
class SearchMixin extends base_core_sdk_1.BaseCoreSDK {
    /**
     * Search for products matching the given keywords.
     *
     * By default, only products that are currently valid (based on their validity dates) are returned.
     * This behavior can be controlled via the `includeInvalidOffers` option in the queryVars parameter.
     *
     * @param keywords - List of keywords to match against product title, description, and tags.
     * @param queryVars - Optional query variables including pagination (productsSkip, productsFirst),
     *                   ordering (productsOrderBy, productsOrderDirection), filtering (productsFilter),
     *                   and includeInvalidOffers flag to control validity filtering.
     * @returns A promise that resolves to an array of product search result fragments.
     */
    searchProducts(keywords, queryVars) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!keywords || keywords.length === 0) {
                return [];
            }
            const now = Math.floor(Date.now() / 1000);
            const validOffersFilter = {
                allVariantsVoided_not: true,
                minValidFromDate_lte: now + 60 + "", // Add 1 minute to ensure we include offers not valid yet, but valid in a very little time
                maxValidUntilDate_gte: now + ""
            };
            const productsSearchFilter = {
                or: keywords
                    .map((keyword) => [
                    { title_contains_nocase: keyword },
                    { description_contains_nocase: keyword },
                    { details_tags_contains_nocase: [keyword] }
                ])
                    .flat()
            };
            const productsFilter = (queryVars === null || queryVars === void 0 ? void 0 : queryVars.includeInvalidOffers)
                ? (queryVars === null || queryVars === void 0 ? void 0 : queryVars.productsFilter)
                    ? {
                        and: [
                            Object.assign({}, queryVars.productsFilter),
                            productsSearchFilter
                        ]
                    }
                    : productsSearchFilter
                : {
                    and: [
                        Object.assign(Object.assign({}, validOffersFilter), queryVars === null || queryVars === void 0 ? void 0 : queryVars.productsFilter),
                        productsSearchFilter
                    ]
                };
            return (0, subgraph_1.searchProducts)(this._subgraphUrl, Object.assign(Object.assign({}, queryVars), { productsFilter }));
        });
    }
}
exports.SearchMixin = SearchMixin;
//# sourceMappingURL=mixin.js.map