"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VIEW_PIPELINE_UTILS = void 0;
const semver_1 = __importDefault(require("semver"));
const MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_DE = '8.0.0';
const MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_COMPASS = '8.1.0';
const isPipelineSearchQueryable = (pipeline) => {
    for (const stage of pipeline) {
        const stageKey = Object.keys(stage)[0];
        if (!(stageKey === '$addFields' ||
            stageKey === '$set' ||
            stageKey === '$match')) {
            return false;
        }
        if (stageKey === '$match') {
            const matchStage = stage['$match'];
            const matchKeys = Object.keys(matchStage || {});
            if (matchKeys.length !== 1 || !matchKeys.includes('$expr')) {
                return false;
            }
        }
    }
    return true;
};
const isVersionSearchCompatibleForViewsDataExplorer = (serverVersion) => {
    try {
        return semver_1.default.gte(serverVersion, MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_DE);
    }
    catch {
        return false;
    }
};
const isVersionSearchCompatibleForViewsCompass = (serverVersion) => {
    try {
        return semver_1.default.gte(serverVersion, MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_COMPASS);
    }
    catch {
        return false;
    }
};
exports.VIEW_PIPELINE_UTILS = {
    MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_DE,
    MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_COMPASS,
    isPipelineSearchQueryable,
    isVersionSearchCompatibleForViewsDataExplorer,
    isVersionSearchCompatibleForViewsCompass,
};
//# sourceMappingURL=views.js.map