"use strict";
/*
 * Copyright OpenSearch Contributors
 * SPDX-License-Identifier: Apache-2.0
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.pplLintHoverProvider = exports.LINT_OWNER = void 0;
const monaco_1 = require("../../../monaco");
const diagnostic_to_marker_1 = require("../diagnostic_to_marker");
const hover_registry_1 = require("./hover_registry");
const engine_outcomes_1 = require("./engine_outcomes");
const hover_card_1 = require("./hover_card");
exports.LINT_OWNER = 'PPL_LINT';
function severityLabel(severity) {
    switch (severity) {
        case monaco_1.monaco.MarkerSeverity.Error:
            return 'Error';
        case monaco_1.monaco.MarkerSeverity.Warning:
            return 'Warning';
        default:
            return 'Info';
    }
}
function ruleIdOf(marker) {
    const code = marker.code;
    if (typeof code === 'string')
        return code;
    return code && typeof code === 'object' && typeof code.value === 'string'
        ? code.value
        : undefined;
}
function docUrlOf(marker) {
    const code = marker.code;
    return code && typeof code === 'object' && code.target ? code.target.toString() : undefined;
}
function markerContainsPosition(marker, position) {
    const { lineNumber, column } = position;
    return !(lineNumber < marker.startLineNumber ||
        lineNumber > marker.endLineNumber ||
        (lineNumber === marker.startLineNumber && column < marker.startColumn) ||
        (lineNumber === marker.endLineNumber && column > marker.endColumn));
}
function markerSpan(marker) {
    return ((marker.endLineNumber - marker.startLineNumber) * 100000 +
        (marker.endColumn - marker.startColumn));
}
exports.pplLintHoverProvider = {
    provideHover(model, position) {
        const markers = monaco_1.monaco.editor
            .getModelMarkers({ owner: exports.LINT_OWNER, resource: model.uri })
            .filter((marker) => marker.source === diagnostic_to_marker_1.LINT_MARKER_SOURCE)
            .filter((marker) => markerContainsPosition(marker, position));
        if (markers.length === 0) {
            return null;
        }
        const marker = markers.reduce((a, b) => (markerSpan(b) < markerSpan(a) ? b : a));
        const ruleId = ruleIdOf(marker);
        const facts = (0, hover_registry_1.getModelHoverFacts)(model, (0, hover_registry_1.markerFixKey)(marker));
        return {
            range: {
                startLineNumber: marker.startLineNumber,
                startColumn: marker.startColumn,
                endLineNumber: marker.endLineNumber,
                endColumn: marker.endColumn,
            },
            contents: [
                {
                    value: (0, hover_card_1.renderHoverCard)({
                        ruleId: ruleId ?? 'ppl-lint',
                        severityLabel: severityLabel(marker.severity),
                        message: marker.message,
                        docUrl: docUrlOf(marker),
                        content: ruleId ? (0, engine_outcomes_1.getRuleHoverContent)(ruleId) : undefined,
                        facts,
                    }),
                    isTrusted: false,
                },
            ],
        };
    },
};
//# sourceMappingURL=hover_provider.js.map