"use strict";
/*
 * Copyright OpenSearch Contributors
 * SPDX-License-Identifier: Apache-2.0
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.setPPLLintEnabled = setPPLLintEnabled;
exports.isPPLLintEnabled = isPPLLintEnabled;
exports.registerPPLLintBridge = registerPPLLintBridge;
exports.setPPLLintContext = setPPLLintContext;
exports.getPPLLintContext = getPPLLintContext;
exports.clearPPLLintContext = clearPPLLintContext;
exports.resolvePPLLintResult = resolvePPLLintResult;
const PPL_LINT_GLOBAL_STATE_KEY = '__osdPPLLintGlobalState';
function getGlobalLintState() {
    const globalScope = globalThis;
    if (!globalScope[PPL_LINT_GLOBAL_STATE_KEY]) {
        globalScope[PPL_LINT_GLOBAL_STATE_KEY] = {
            bridge: undefined,
            contexts: new WeakMap(),
            // Off by default: lint is gated by the queryEnhancements.pplLint capability
            // (off by default). The plugin opts in via setPPLLintEnabled(true).
            enabled: false,
        };
    }
    return globalScope[PPL_LINT_GLOBAL_STATE_KEY];
}
function setPPLLintEnabled(enabled) {
    getGlobalLintState().enabled = enabled;
}
function isPPLLintEnabled() {
    return getGlobalLintState().enabled;
}
function registerPPLLintBridge(bridge) {
    const state = getGlobalLintState();
    state.bridge = bridge;
    return () => {
        if (state.bridge === bridge) {
            state.bridge = undefined;
        }
    };
}
function setPPLLintContext(model, context) {
    getGlobalLintState().contexts.set(model, context);
}
function getPPLLintContext(model) {
    return getGlobalLintState().contexts.get(model);
}
function clearPPLLintContext(model) {
    getGlobalLintState().contexts.delete(model);
}
async function resolvePPLLintResult(model, content, fallbackLint) {
    const state = getGlobalLintState();
    if (state.bridge) {
        try {
            const runtimeResult = await state.bridge({
                content,
                model,
                context: state.contexts.get(model),
            });
            if (runtimeResult != null) {
                return runtimeResult;
            }
        }
        catch {
            // fall through to compiled fallback
        }
    }
    return fallbackLint(content);
}
//# sourceMappingURL=lint_bridge.js.map