"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useCuration = void 0;
const react_1 = __importDefault(require("react"));
const hooks_1 = require("../hooks");
const utility_1 = require("../utility");
const useCuration = () => {
    const { options, config } = (0, hooks_1.useReactifySearchContext)();
    const { searchTerm } = (0, hooks_1.useSearch)();
    const curation = react_1.default.useMemo(() => {
        const handleOrSearchTerm = options.mode === "collection" ? options.collectionHandle : searchTerm;
        const normalisedHandleOrSearchTerm = handleOrSearchTerm
            .toLowerCase()
            .trim();
        const globalCuration = config.curations.find((curation) => {
            if (options.mode === "search") {
                if (options.market) {
                    return curation.id === `global_${options.market}_search`;
                }
                else {
                    return curation.id === "global_search";
                }
            }
            if (options.mode === "instant-search") {
                if (options.market) {
                    return curation.id === `global_${options.market}_search`;
                }
                else {
                    return curation.id === "global_search";
                }
            }
            if (options.mode === "collection") {
                if (options.market) {
                    return curation.id === `global_${options.market}_collection`;
                }
                else {
                    return curation.id === "global_collection";
                }
            }
            return false;
        });
        const curation = config.curations.find((curation) => {
            var _a, _b;
            if (options.market) {
                if (curation.markets && !curation.markets.includes(options.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.mode) {
                return ("search" === curation.type &&
                    normalisedHandleOrSearchTerm === normalisedSearchTerm);
            }
            if ("search" === options.mode) {
                return ("search" === curation.type &&
                    normalisedHandleOrSearchTerm === normalisedSearchTerm);
            }
            if ("collection" === options.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;
        };
        return curation
            ? addGlobalBoosting(curation)
            : globalCuration
                ? globalCuration
                : undefined;
    }, [
        config,
        options.mode === "collection" ? options.collectionHandle : undefined,
        searchTerm,
    ]);
    react_1.default.useEffect(() => {
        utility_1.debug.log("useCuration", "curation", curation);
    }, [curation]);
    return {
        curation: curation,
    };
};
exports.useCuration = useCuration;
//# sourceMappingURL=useCuration.js.map