import { CodeCompletionCore } from 'antlr4-c3';
import { ParseErrorListener } from '../common/parseErrorListener';
import { ImpalaSqlParser } from '../../lib/impala/ImpalaSqlParser';
export class ImpalaErrorListener extends ParseErrorListener {
    constructor(errorListener, preferredRules, locale) {
        super(errorListener, locale);
        this.objectNames = new Map([
            [ImpalaSqlParser.RULE_databaseNamePath, 'database'],
            [ImpalaSqlParser.RULE_databaseNameCreate, 'database'],
            [ImpalaSqlParser.RULE_tableNamePath, 'table'],
            [ImpalaSqlParser.RULE_tableNameCreate, 'table'],
            [ImpalaSqlParser.RULE_viewNamePath, 'view'],
            [ImpalaSqlParser.RULE_viewNameCreate, 'view'],
            [ImpalaSqlParser.RULE_functionNamePath, 'function'],
            [ImpalaSqlParser.RULE_functionNameCreate, 'function'],
            [ImpalaSqlParser.RULE_columnNamePath, 'column'],
            [ImpalaSqlParser.RULE_columnNamePathCreate, '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 ImpalaSqlParser.RULE_databaseNamePath:
                    case ImpalaSqlParser.RULE_tableNamePath:
                    case ImpalaSqlParser.RULE_functionNamePath:
                    case ImpalaSqlParser.RULE_viewNamePath:
                    case ImpalaSqlParser.RULE_columnNamePath: {
                        result.push(`{existing}${name}`);
                        break;
                    }
                    case ImpalaSqlParser.RULE_databaseNameCreate:
                    case ImpalaSqlParser.RULE_tableNameCreate:
                    case ImpalaSqlParser.RULE_functionNameCreate:
                    case ImpalaSqlParser.RULE_viewNameCreate:
                    case ImpalaSqlParser.RULE_columnNamePathCreate: {
                        result.push(`{new}${name}`);
                        break;
                    }
                }
            }
            expectedText = result.join(`{or}`);
        }
        if (candidates.tokens.size) {
            expectedText += expectedText ? '{orKeyword}' : '{keyword}';
        }
        return expectedText;
    }
}
