import { CodeCompletionCore } from 'antlr4-c3';
import { ParseErrorListener } from '../common/parseErrorListener';
import { HiveSqlParser } from '../../lib/hive/HiveSqlParser';
export class HiveErrorListener extends ParseErrorListener {
    constructor(errorListener, preferredRules, locale) {
        super(errorListener, locale);
        this.objectNames = new Map([
            [HiveSqlParser.RULE_dbSchemaName, 'database'],
            [HiveSqlParser.RULE_dbSchemaNameCreate, 'database'],
            [HiveSqlParser.RULE_tableName, 'table'],
            [HiveSqlParser.RULE_tableNameCreate, 'table'],
            [HiveSqlParser.RULE_viewName, 'view'],
            [HiveSqlParser.RULE_viewNameCreate, 'view'],
            [HiveSqlParser.RULE_functionNameForDDL, 'function'],
            [HiveSqlParser.RULE_functionNameForInvoke, 'function'],
            [HiveSqlParser.RULE_functionNameCreate, 'function'],
            [HiveSqlParser.RULE_columnName, 'column'],
            [HiveSqlParser.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 HiveSqlParser.RULE_dbSchemaName:
                    case HiveSqlParser.RULE_tableName:
                    case HiveSqlParser.RULE_viewName:
                    case HiveSqlParser.RULE_functionNameForDDL:
                    case HiveSqlParser.RULE_functionNameForInvoke:
                    case HiveSqlParser.RULE_columnName: {
                        result.push(`{existing}${name}`);
                        break;
                    }
                    case HiveSqlParser.RULE_dbSchemaNameCreate:
                    case HiveSqlParser.RULE_tableNameCreate:
                    case HiveSqlParser.RULE_functionNameCreate:
                    case HiveSqlParser.RULE_viewNameCreate:
                    case HiveSqlParser.RULE_columnNameCreate: {
                        result.push(`{new}${name}`);
                        break;
                    }
                }
            }
            expectedText = result.join('{or}');
        }
        if (candidates.tokens.size) {
            expectedText += expectedText ? '{orKeyword}' : '{keyword}';
        }
        return expectedText;
    }
}
