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