import { CodeCompletionCore } from 'antlr4-c3';
import { ParseErrorListener } from '../common/parseErrorListener';
import { MySqlParser } from '../../lib/mysql/MySqlParser';
export class MysqlErrorListener extends ParseErrorListener {
    constructor(errorListener, preferredRules, locale) {
        super(errorListener, locale);
        this.objectNames = new Map([
            [MySqlParser.RULE_databaseName, 'database'],
            [MySqlParser.RULE_databaseNameCreate, 'database'],
            [MySqlParser.RULE_tableName, 'table'],
            [MySqlParser.RULE_tableNameCreate, 'table'],
            [MySqlParser.RULE_viewName, 'view'],
            [MySqlParser.RULE_viewNameCreate, 'view'],
            [MySqlParser.RULE_functionName, 'function'],
            [MySqlParser.RULE_functionNameCreate, 'function'],
            [MySqlParser.RULE_columnName, 'column'],
            [MySqlParser.RULE_columnNameCreate, 'column'],
        ]);
        this.preferredRules = preferredRules;
    }
    getExpectedText(parser, token) {
        var _a;
        let expectedText = '';
        let currentContext = (_a = parser.context) !== null && _a !== void 0 ? _a : undefined;
        while (currentContext === null || currentContext === void 0 ? void 0 : currentContext.parent) {
            currentContext = currentContext.parent;
        }
        const core = new CodeCompletionCore(parser);
        core.preferredRules = this.preferredRules;
        const candidates = core.collectCandidates(token.tokenIndex, currentContext);
        if (candidates.rules.size) {
            const result = [];
            // get expectedText as collect rules first
            for (const candidate of candidates.rules) {
                const [ruleType] = candidate;
                const name = this.objectNames.get(ruleType);
                switch (ruleType) {
                    case MySqlParser.RULE_databaseName:
                    case MySqlParser.RULE_tableName:
                    case MySqlParser.RULE_functionName:
                    case MySqlParser.RULE_viewName:
                    case MySqlParser.RULE_columnName: {
                        result.push(`{existing}${name}`);
                        break;
                    }
                    case MySqlParser.RULE_databaseNameCreate:
                    case MySqlParser.RULE_tableNameCreate:
                    case MySqlParser.RULE_functionNameCreate:
                    case MySqlParser.RULE_viewNameCreate:
                    case MySqlParser.RULE_columnNameCreate: {
                        result.push(`{new}${name}`);
                        break;
                    }
                }
            }
            expectedText = result.join(`{or}`);
        }
        if (candidates.tokens.size) {
            expectedText += expectedText ? '{orKeyword}' : '{keyword}';
        }
        return expectedText;
    }
}
