"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateSortQuery = exports.generateSortSort = exports.getSortOption = exports.getSortOptions = void 0;
const getSortOptions = (options) => {
    return options.config.sort
        .sort((a, b) => {
        const aInt = typeof a.position === "string"
            ? parseInt(a.position, 10)
            : a.position;
        const bInt = typeof b.position === "string"
            ? parseInt(b.position, 10)
            : b.position;
        return aInt - bInt;
    })
        .filter(({ visibility }) => ["all", options.provider.mode].includes(visibility));
};
exports.getSortOptions = getSortOptions;
const getSortOption = (options) => {
    var _a;
    const sortOptions = (0, exports.getSortOptions)(options);
    return (_a = sortOptions.find(({ handle }) => { var _a; return handle === options.sortOption || handle === ((_a = options.query) === null || _a === void 0 ? void 0 : _a.sort); })) !== null && _a !== void 0 ? _a : sortOptions[0];
};
exports.getSortOption = getSortOption;
const generateSortSort = (options) => {
    var _a, _b;
    // curation positions are only applied for the default `collections.position` or `_score` sort
    // if the sort is something else, apply a normal sort which applies what the user has requested
    if (options.provider.mode !== "instant-search") {
        if (options.sortOption &&
            !["_score", "collections.position"].includes(options.sortOption.field)) {
            return [{ [options.sortOption.field]: options.sortOption.direction }];
        }
    }
    const sorts = [];
    // collection curation pin sorting
    if (options.provider.mode === "collection" && options.provider.collectionHandle) {
        if ((_a = options.curation) === null || _a === void 0 ? void 0 : _a.collectionHandle) {
            const queries = [{
                    term: {
                        "curations.collectionHandle.keyword": options.curation.collectionHandle,
                    },
                }];
            if (options.provider.market) {
                queries.push({
                    term: {
                        "curations.markets.keyword": options.provider.market,
                    },
                });
            }
            sorts.push({
                "curations.position": {
                    unmapped_type: "long",
                    order: "asc",
                    nested: {
                        path: "curations",
                        filter: {
                            bool: {
                                must: queries
                            }
                        }
                    },
                },
            });
        }
    }
    // search options.curation pin sorting
    if (options.provider.mode === "search" || options.provider.mode === "instant-search") {
        if ((_b = options.curation) === null || _b === void 0 ? void 0 : _b.searchTerm) {
            const queries = [{
                    term: {
                        "curations.searchTerm.keyword": options.curation.searchTerm.toLowerCase(),
                    },
                }];
            if (options.provider.market) {
                queries.push({
                    term: {
                        "curations.markets.keyword": options.provider.market,
                    },
                });
            }
            sorts.push({
                "curations.position": {
                    unmapped_type: "long",
                    order: "asc",
                    nested: {
                        path: "curations",
                        filter: {
                            bool: {
                                must: queries
                            }
                        }
                    },
                },
            });
        }
    }
    // options.curation boost sorting
    if (options.curation) {
        if (0 < options.curation.boosting.groupings.length) {
            const groupings = options.curation.boosting.groupings.sort((a, b) => a.position > b.position ? 1 : -1);
            for (const grouping of groupings) {
                try {
                    const query = JSON.parse(grouping.query);
                    if (query)
                        sorts.push(query);
                }
                catch (_c) {
                    console.error(`query could not be parsed for boost grouping`, grouping);
                }
            }
        }
        if (0 < options.curation.boosting.sortings.length) {
            const sortings = options.curation.boosting.sortings.sort((a, b) => a.position > b.position ? 1 : -1);
            for (const sorting of sortings) {
                try {
                    const query = JSON.parse(sorting.query);
                    if (query)
                        sorts.push(query);
                }
                catch (_d) {
                    console.error(`query could not be parsed for boost sorting`, sorting);
                }
            }
        }
    }
    // default collection sorting
    if (options.provider.mode === "collection" && options.provider.collectionHandle) {
        sorts.push({
            "collections.position": {
                order: "asc",
                nested: {
                    path: "collections",
                    filter: {
                        term: {
                            "collections.handle.keyword": options.provider.collectionHandle,
                        },
                    },
                },
            },
        });
    }
    // default search sorting
    if (options.provider.mode === "search" || options.provider.mode === "instant-search") {
        sorts.push("_score");
    }
    return sorts;
};
exports.generateSortSort = generateSortSort;
const generateSortQuery = (options) => {
    var _a;
    if (!options.curation || options.curation.id.includes("global")) {
        return { match_all: {} };
    }
    const queries = [{
            term: {
                [`curations.${"collection" === options.curation.type
                    ? "collectionHandle"
                    : "searchTerm"}.keyword`]: "collection" === options.curation.type
                    ? options.curation.collectionHandle
                    : (_a = options.curation.searchTerm) === null || _a === void 0 ? void 0 : _a.toLowerCase(),
            },
        },
        {
            term: {
                "curations.hidden": true,
            },
        }];
    if (options.provider.market) {
        queries.push({
            term: {
                "curations.markets.keyword": options.provider.market,
            },
        });
    }
    return {
        bool: {
            must_not: [
                {
                    nested: {
                        path: "curations",
                        query: {
                            bool: {
                                must: queries,
                            },
                        },
                    },
                },
                // hide callout when not sorting by _score or collections.position
                ...(options.sortOption &&
                    !["_score", "collections.position"].includes(options.sortOption.field)
                    ? [
                        {
                            term: {
                                type: "callout",
                            },
                        },
                    ]
                    : []),
            ],
        },
    };
};
exports.generateSortQuery = generateSortQuery;
//# sourceMappingURL=sortOption.js.map