// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore There is a default export but not in the types
import antlrDefaultExport, { CommonTokenStream, } from 'antlr4';
import CypherLexer from './generated-parser/CypherCmdLexer';
import CypherParser, { NodePatternContext, RelationshipPatternContext, } from './generated-parser/CypherCmdParser';
export function findStopNode(root) {
    let children = root.children;
    let current = root;
    while (children && children.length > 0) {
        let index = children.length - 1;
        let child = children[index];
        while (index > 0 &&
            (child === root.EOF() ||
                child.getText() === '' ||
                child.getText().startsWith('<missing'))) {
            index--;
            child = children[index];
        }
        current = child;
        children = current.children;
    }
    return current;
}
export function findParent(leaf, condition) {
    let current = leaf;
    while (current && !condition(current)) {
        current = current.parentCtx;
    }
    return current;
}
export function isDefined(x) {
    return x !== null && x !== undefined;
}
export const antlrUtils = antlrDefaultExport;
export function inNodeLabel(stopNode) {
    const nodePattern = findParent(stopNode, (p) => p instanceof NodePatternContext ||
        p instanceof RelationshipPatternContext);
    return nodePattern instanceof NodePatternContext;
}
export function inRelationshipType(stopNode) {
    const relPattern = findParent(stopNode, (p) => p instanceof NodePatternContext ||
        p instanceof RelationshipPatternContext);
    return relPattern instanceof RelationshipPatternContext;
}
export function findCaret(parsingResult, caretPosition) {
    const statements = parsingResult.statementsParsing;
    let i = 0;
    let result = undefined;
    let keepLooking = true;
    while (i < statements.length && keepLooking) {
        let j = 0;
        const statement = statements[i];
        const tokens = statement.tokens;
        while (j < tokens.length && keepLooking) {
            const currentToken = tokens[j];
            keepLooking = currentToken.start <= caretPosition;
            if (currentToken.channel === 0 && keepLooking) {
                result = { statement: statement, token: currentToken };
            }
            j++;
        }
        i++;
    }
    return result;
}
export function splitIntoStatements(tokenStream, lexer) {
    tokenStream.fill();
    const tokens = tokenStream.tokens;
    let i = 0;
    const result = [];
    let chunk = [];
    let offset = 0;
    while (i < tokens.length) {
        const current = tokens[i].clone();
        current.tokenIndex -= offset;
        chunk.push(current);
        if (current.type === CypherLexer.SEMICOLON ||
            current.type === CypherLexer.EOF) {
            // This does not relex since we are not calling fill on the token stream
            const tokenStream = new CommonTokenStream(lexer);
            tokenStream.tokens = chunk;
            result.push(tokenStream);
            offset = i + 1;
            chunk = [];
        }
        i++;
    }
    return result;
}
export function findPreviousNonSpace(tokens, index) {
    let i = index;
    while (i > 0) {
        const token = tokens[--i];
        if (token.type !== CypherParser.SPACE) {
            return token;
        }
    }
    return undefined;
}
export function isCommentOpener(thisToken, nextToken) {
    return thisToken.text === '/' && nextToken?.text === '*';
}
export function resolveCypherVersion(parsedVersion, dbSchema) {
    const cypherVersion = parsedVersion ?? dbSchema.defaultLanguage ?? 'CYPHER 5';
    return cypherVersion;
}
export const rulesDefiningVariables = [
    CypherParser.RULE_returnItem,
    CypherParser.RULE_unwindClause,
    CypherParser.RULE_subqueryInTransactionsReportParameters,
    CypherParser.RULE_procedureResultItem,
    CypherParser.RULE_foreachClause,
    CypherParser.RULE_loadCSVClause,
    CypherParser.RULE_reduceExpression,
    CypherParser.RULE_listItemsPredicate,
    CypherParser.RULE_listComprehension,
];
export const rulesDefiningOrUsingVariables = [
    ...rulesDefiningVariables,
    CypherParser.RULE_pattern,
    CypherParser.RULE_nodePattern,
    CypherParser.RULE_relationshipPattern,
];
//# sourceMappingURL=helpers.js.map