"use strict";
/*
 * Copyright OpenSearch Contributors
 * SPDX-License-Identifier: Apache-2.0
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.unionMinDatasetsDetector = void 0;
const rule_index_1 = require("../rule_index");
const range_utils_1 = require("../range_utils");
// Engine ground truth: union with fewer than two datasets throws
// "Union command requires at least two datasets" (CalciteRelNodeVisitor).
// Runtime-only + Calcite-gated, minVersion 3.7.0.
//
// `unionCommand` is legal both query-initial (grammar rule `pplCommands`) and
// mid-pipeline (grammar rule `commands`). Mid-pipeline, the upstream pipeline is
// the implicit first dataset (Union.attach prepends it; Calcite counts it), so a
// single explicit `unionDataset` is valid — e.g. `... | union [subsearch]`. We
// only enforce the ≥2 rule when the union is query-initial, discriminated by a
// `pplCommands` parent. A positive gate keeps us silent whenever the parent can't
// be resolved to `pplCommands` (fragments, pipe-first parses), which is the safe
// default for an error-severity rule.
const unionMinDatasetsDetector = (tree, config, context, ruleNameToIndex) => {
    if (context.isCalcite !== true) {
        return [];
    }
    const pplCommandsIndex = ruleNameToIndex('pplCommands');
    if (pplCommandsIndex === -1) {
        return [];
    }
    const diagnostics = [];
    // `unionCommand` is runtime-only; absent on the compiled surface → [].
    const commands = (0, rule_index_1.findAllDescendantsByRule)(tree, ruleNameToIndex, 'unionCommand');
    for (const command of commands) {
        const parent = command.parent;
        const isQueryInitial = (0, rule_index_1.isRuleNode)(parent) && parent.ruleIndex === pplCommandsIndex;
        if (!isQueryInitial) {
            continue;
        }
        const datasets = (0, rule_index_1.findAllChildrenByRule)(command, ruleNameToIndex, 'unionDataset');
        if (datasets.length < 2) {
            diagnostics.push({
                ruleId: config.id,
                severity: config.severity,
                message: config.message,
                range: (0, range_utils_1.rangeFromContext)(command),
                docUrl: config.docUrl,
            });
        }
    }
    return diagnostics;
};
exports.unionMinDatasetsDetector = unionMinDatasetsDetector;
//# sourceMappingURL=union_min_datasets.js.map