"use strict";
/*
 * Copyright OpenSearch Contributors
 * SPDX-License-Identifier: Apache-2.0
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateCatalogEntry = validateCatalogEntry;
exports.loadCatalog = loadCatalog;
exports.getBundledCatalog = getBundledCatalog;
const tslib_1 = require("tslib");
const rules_catalog_json_1 = tslib_1.__importDefault(require("./rules_catalog.json"));
// TRACKING (opensearch-project/sql#4549): set maxVersion once that issue ships.
const VALID_SEVERITIES = new Set(['error', 'warning', 'info']);
function isNonEmptyString(value) {
    return typeof value === 'string' && value.length > 0;
}
function isValidAppliesTo(value) {
    if (typeof value !== 'object' || value === null) {
        return false;
    }
    const candidate = value;
    return ((candidate.minVersion === undefined || typeof candidate.minVersion === 'string') &&
        (candidate.maxVersion === undefined || typeof candidate.maxVersion === 'string') &&
        (candidate.engine === undefined || candidate.engine === 'calcite'));
}
function validateCatalogEntry(value) {
    if (typeof value !== 'object' || value === null) {
        return null;
    }
    const candidate = value;
    if (!isNonEmptyString(candidate.id) ||
        !isNonEmptyString(candidate.detector) ||
        typeof candidate.enabled !== 'boolean' ||
        typeof candidate.severity !== 'string' ||
        !VALID_SEVERITIES.has(candidate.severity) ||
        typeof candidate.message !== 'string' ||
        typeof candidate.docUrl !== 'string' ||
        !isValidAppliesTo(candidate.appliesTo)) {
        return null;
    }
    for (const key of ['runtimeOnly', 'needsContext', 'needsExplain']) {
        if (candidate[key] !== undefined && typeof candidate[key] !== 'boolean')
            return null;
    }
    return {
        id: candidate.id,
        detector: candidate.detector,
        enabled: candidate.enabled,
        severity: candidate.severity,
        message: candidate.message,
        docUrl: candidate.docUrl,
        appliesTo: candidate.appliesTo,
        runtimeOnly: candidate.runtimeOnly,
        needsContext: candidate.needsContext,
        needsExplain: candidate.needsExplain,
    };
}
function loadCatalog(entries) {
    if (!Array.isArray(entries)) {
        // eslint-disable-next-line no-console
        console.warn('[ppl-lint] catalog is not an array; loading empty catalog');
        return [];
    }
    const valid = [];
    for (const entry of entries) {
        const parsed = validateCatalogEntry(entry);
        if (parsed) {
            valid.push(parsed);
        }
        else {
            // eslint-disable-next-line no-console
            console.warn('[ppl-lint] dropped malformed catalog entry', entry);
        }
    }
    return valid;
}
let bundledCatalog;
function getBundledCatalog() {
    if (!bundledCatalog) {
        const source = Array.isArray(rules_catalog_json_1.default)
            ? rules_catalog_json_1.default
            : rules_catalog_json_1.default.default;
        bundledCatalog = loadCatalog(source);
    }
    return bundledCatalog;
}
//# sourceMappingURL=catalog.js.map