"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ParseErrorListener = void 0;
const FlinkSQLLexer_1 = require("../lib/FlinkSQLLexer");
const protocol_translation_1 = require("../protocol-translation");
class ParseErrorListener {
    constructor(document) {
        this.document = document;
        this._errors = [];
    }
    getErrors() {
        return this._errors;
    }
    syntaxError(recognizer, offendingSymbol, line, charPositionInLine, _, e) {
        const suggestions = e?.expectedTokens
            ?.toArray()
            .map(t => {
            return FlinkSQLLexer_1.FlinkSQLLexer.VOCABULARY.getDisplayName(t).replace(/'/g, '') || '';
        })
            .filter(w => !!w) || [];
        const toMessage = (word) => {
            let message = `SQL parse failed. A problem is encountered around "${word}" at line ${line}, column ${charPositionInLine + 1}. `;
            if (suggestions.length) {
                message += `Do you mean:\n  ${suggestions.map(s => `"${s}"`).join(', ')}`;
            }
            else {
                message += `There are no viable suggestions.`;
            }
            return message;
        };
        if (offendingSymbol?.text) {
            this._errors.push({
                message: toMessage(offendingSymbol.text),
                severity: (0, protocol_translation_1.toDiagnosticSeverity)('error'),
                source: 'Language Server',
                range: {
                    start: this.document.positionAt(offendingSymbol.startIndex),
                    end: this.document.positionAt(offendingSymbol.stopIndex + 1)
                },
                data: {
                    suggestions
                }
            });
        }
    }
}
exports.ParseErrorListener = ParseErrorListener;
//# sourceMappingURL=parse-error.listener.js.map