"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 findCuration = () => {
        const curationsMatchingQuery = options.config.curations.filter((curation) => {
            var _a, _b;
            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 curationsMatchingMarketOrNoMarket = curationsMatchingQuery.filter((curation) => {
            var _a, _b, _c;
            if (options.provider.market) {
                if (((_a = curation.markets) === null || _a === void 0 ? void 0 : _a.length) === 0) {
                    return true;
                }
                else {
                    return (_b = curation.markets) === null || _b === void 0 ? void 0 : _b.includes(options.provider.market);
                }
            }
            else {
                return ((_c = curation.markets) === null || _c === void 0 ? void 0 : _c.length) === 0;
            }
        }).sort((curationA, curationB) => {
            var _a, _b, _c, _d;
            return ((_b = (_a = curationB.markets) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) - ((_d = (_c = curationA.markets) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0);
        });
        return curationsMatchingMarketOrNoMarket[0];
    };
    const curation = findCuration();
    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