"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const __1 = require("../../..");
const symbolElement_1 = require("../../../symbols/symbolElement");
class AbstractParseService {
    findSymbolElementByTokenInSymbolTable(tokenInAST) {
        let tableAllLevel = this.symbolTable.getAllLevel();
        for (let level of tableAllLevel) {
            let table = this.symbolTable.getTable(level);
            let keys = table.keys();
            for (let startIndex of keys) {
                let element = table.get(startIndex);
                if (element.startIndex == tokenInAST.startIndex &&
                    element.stopIndex == tokenInAST.stopIndex) {
                    return element;
                }
            }
        }
    }
    findTokenByRangeOffSetInAST(tokenStream) {
        let tokens = tokenStream.getTokens();
        for (let token of tokens) {
            if (this.editorContext.rangeOffset >= token.startIndex &&
                this.editorContext.rangeOffset <= token.stopIndex) {
                return token;
            }
        }
    }
    generateCodeCompletionRequest(element) {
        let completionRequest = new __1.CodeCompletionRequest();
        if (element.type == symbolElement_1.ElementType.TABLE) {
            completionRequest.cpId = this.editorContext.cpId;
            completionRequest.currentCommandType = this.editorContext.editorType;
            completionRequest.expectType = symbolElement_1.ElementType.TABLE;
            completionRequest.dataBaseName = this.editorContext.dataBaseName;
        }
        else if (element.type == symbolElement_1.ElementType.COLUMN) {
            completionRequest.cpId = this.editorContext.cpId;
            completionRequest.currentCommandType = this.editorContext.editorType;
            completionRequest.expectType = symbolElement_1.ElementType.COLUMN;
            completionRequest.dataBaseName = this.editorContext.dataBaseName;
            let tableName = this.findTableName(element);
            if (tableName == undefined) {
                tableName = "ALL_TABLE";
            }
            completionRequest.tableName = tableName;
        }
        return completionRequest;
    }
    findTableName(currentElement) {
        let table = this.symbolTable.getTable(currentElement.level);
        for (let element of table.values()) {
            if (element.type == symbolElement_1.ElementType.TABLE) {
                if (currentElement.alias == undefined) {
                    return this.findText(element);
                }
                else {
                    if (currentElement.alias == element.alias ||
                        currentElement.alias == element.text) {
                        return this.findText(element);
                    }
                }
            }
        }
    }
    findText(element) {
        //如果只有别名没有表名，则需要去下一级最后一个作用域中查找
        if (element.text == undefined && element.alias != undefined) {
            let currentLevel = element.level;
            let childLevel = currentLevel + "::" + 1;
            let childTable = this.symbolTable.getTable(childLevel);
            if (childTable == undefined) {
                return;
            }
            for (let childElement of childTable.values()) {
                if (childElement.type == symbolElement_1.ElementType.TABLE) {
                    return this.findText(childElement);
                }
            }
        }
        return element.text;
    }
}
exports.AbstractParseService = AbstractParseService;
