"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFilterStack = void 0;
const getFilterStack = (options) => {
    // select filters by type
    const matchingFilterss = options.config.filters.filter((filter) => filter.type === options.provider.mode);
    // select filter stack by handle if provided
    if (options.provider.filtersHandle) {
        const matchingFilters = matchingFilterss.find((filterStack) => options.provider.filtersHandle === filterStack.handle);
        if (matchingFilters) {
            return matchingFilters;
        }
    }
    // select filter stack by collection if provided
    if (options.provider.mode === "collection" && options.provider.collectionHandle) {
        const matchingFilters = matchingFilterss.find((filterStack) => { var _a; return (_a = filterStack.collections) === null || _a === void 0 ? void 0 : _a.includes(options.provider.mode === "collection" ? options.provider.collectionHandle : ""); });
        if (matchingFilters) {
            return matchingFilters;
        }
    }
    // select filter with "default" handle
    let matchingFilters = matchingFilterss.find((filterStack) => filterStack.handle === "default");
    if (matchingFilters) {
        return matchingFilters;
    }
    // select any filter with "default" handle
    matchingFilters = options.config.filters.find((filterStack) => filterStack.handle === "default");
    if (matchingFilters) {
        return matchingFilters;
    }
    // select any available filter as a last resort
    matchingFilters = options.config.filters[0];
    return matchingFilters;
};
exports.getFilterStack = getFilterStack;
//# sourceMappingURL=filters.js.map