"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCuration = void 0;
const getCuration = (options) => {
    var _a;
    const handleOrSearchTerm = options.provider.mode === "collection" ? options.provider.collectionHandle : (_a = options.searchTerm) !== null && _a !== void 0 ? _a : "";
    const normalisedHandleOrSearchTerm = handleOrSearchTerm
        .toLowerCase()
        .trim();
    const globalCuration = options.config.curations.find((curation) => {
        if (options.provider.mode === "search") {
            if (options.provider.market) {
                return curation.id === `global_${options.provider.market}_search`;
            }
            else {
                return curation.id === "global_search";
            }
        }
        if (options.provider.mode === "instant-search") {
            if (options.provider.market) {
                return curation.id === `global_${options.provider.market}_search`;
            }
            else {
                return curation.id === "global_search";
            }
        }
        if (options.provider.mode === "collection") {
            if (options.provider.market) {
                return curation.id === `global_${options.provider.market}_collection`;
            }
            else {
                return curation.id === "global_collection";
            }
        }
        return false;
    });
    const curation = options.config.curations.find((curation) => {
        var _a, _b;
        if (options.provider.market) {
            if (curation.markets && !curation.markets.includes(options.provider.market)) {
                return false;
            }
        }
        const normalisedSearchTerm = (_a = curation.searchTerm) === null || _a === void 0 ? void 0 : _a.toLowerCase().trim();
        const normalisedCollectionHandle = (_b = curation.collectionHandle) === null || _b === void 0 ? void 0 : _b.toLowerCase().trim();
        if ("instant-search" === options.provider.mode) {
            return ("search" === curation.type &&
                normalisedHandleOrSearchTerm === normalisedSearchTerm);
        }
        if ("search" === options.provider.mode) {
            return ("search" === curation.type &&
                normalisedHandleOrSearchTerm === normalisedSearchTerm);
        }
        if ("collection" === options.provider.mode) {
            return ("collection" === curation.type &&
                normalisedHandleOrSearchTerm === normalisedCollectionHandle);
        }
        return false;
    });
    const addGlobalBoosting = (curation) => {
        if (!globalCuration) {
            return curation;
        }
        const curationHasRules = !!curation.boosting.groupings.length ||
            !!curation.boosting.sortings.length;
        if (curationHasRules) {
            return curation;
        }
        const curationWithGlobalBoosting = Object.assign(Object.assign({}, curation), { boosting: globalCuration.boosting });
        return curationWithGlobalBoosting;
    };
    if (curation) {
        return addGlobalBoosting(curation);
    }
    if (globalCuration) {
        return globalCuration;
    }
    return undefined;
};
exports.getCuration = getCuration;
//# sourceMappingURL=curation.js.map