"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SpaceReplacerFormatVisitor = void 0;
const tree_1 = require("antlr4ts/tree");
const protocol_translation_1 = require("../protocol-translation");
class SpaceReplacerFormatVisitor extends tree_1.AbstractParseTreeVisitor {
    constructor(tokens, config) {
        super();
        this.tokens = tokens;
        this.config = config;
        this.textEdits = [];
        this.indentNumber = 0;
        this.indentString = '';
        this.indentString = ' '.repeat(this.config.tabSize);
        this.maxParenthesisLength = 40;
        this.newline = '\n';
        this.newlineRegex = new RegExp(this.newline);
    }
    defaultResult() {
        return;
    }
    get indent() {
        return this.indentString.repeat(this.indentNumber);
    }
    get newlineIndent() {
        return this.newline + this.indent;
    }
    addIndent() {
        this.indentNumber++;
    }
    minusIndent() {
        if (this.indentNumber === 0) {
            return;
        }
        this.indentNumber--;
    }
    updateBeforeNextTokenByContext(ctx, newText) {
        const range = (0, protocol_translation_1.rangeFromContext)(ctx);
        const lastToken = ctx._stop;
        if (lastToken) {
            const nextToken = this.tokens.get(lastToken.tokenIndex + 1);
            if (nextToken) {
                this.textEdits.push({
                    range: {
                        start: range.end,
                        end: { line: nextToken.line - 1, character: nextToken.charPositionInLine }
                    },
                    newText
                });
            }
        }
    }
    updateAfterPreviousTokenByContext(ctx, newText) {
        const range = (0, protocol_translation_1.rangeFromContext)(ctx);
        const firstToken = ctx._start;
        const previousToken = this.tokens.get(firstToken.tokenIndex - 1);
        if (previousToken) {
            if (!this.newlineRegex.test(newText) && previousToken.channel === 2) {
                newText = this.newlineIndent;
            }
            this.textEdits.push({
                range: {
                    start: {
                        line: previousToken.line - 1,
                        character: previousToken.charPositionInLine + (previousToken.text?.length || 0)
                    },
                    end: range.start
                },
                newText
            });
        }
    }
    updateBeforeNextTokenByNode(node, newText) {
        const nextToken = this.tokens.get(node.symbol.tokenIndex + 1);
        if (nextToken) {
            this.textEdits.push({
                range: {
                    start: { line: node.symbol.line - 1, character: node.symbol.charPositionInLine + node.text.length },
                    end: { line: nextToken.line - 1, character: nextToken.charPositionInLine }
                },
                newText
            });
        }
    }
    updateAfterPreviousTokenByNode(node, newText) {
        const previousToken = this.tokens.get(node.symbol.tokenIndex - 1);
        if (previousToken) {
            if (!this.newlineRegex.test(newText) && previousToken.channel === 2) {
                newText = this.newlineIndent;
            }
            this.textEdits.push({
                range: {
                    start: {
                        line: previousToken.line - 1,
                        character: previousToken.charPositionInLine + (previousToken.text?.length || 0)
                    },
                    end: { line: node.symbol.line - 1, character: node.symbol.charPositionInLine }
                },
                newText
            });
        }
    }
    alignColumnDefinitions(ctx) {
        let longestColumnDefinitionName = 0;
        if (ctx.tableElement().length === 1) {
            const columnName = ctx.tableElement()[0].columnDefinition()?.columnName();
            if (columnName) {
                this.updateBeforeNextTokenByContext(columnName, ' ');
            }
            return;
        }
        ctx.tableElement().forEach(elem => {
            const columnName = elem.columnDefinition()?.columnName();
            if (columnName && longestColumnDefinitionName < columnName.text.length) {
                longestColumnDefinitionName = columnName.text.length;
            }
        });
        ctx.tableElement().forEach((elem, i) => {
            const columnName = elem.columnDefinition()?.columnName();
            if (columnName) {
                const alignCharacter = +(i === 0) + longestColumnDefinitionName - columnName.text.length + 1;
                this.handleAlignNextToken(columnName, alignCharacter);
            }
        });
    }
    handleAlignNextToken(ctx, alignCharacter) {
        const range = (0, protocol_translation_1.rangeFromContext)(ctx);
        const lastToken = ctx._stop;
        if (lastToken) {
            const nextToken = this.tokens.get(lastToken.tokenIndex + 1);
            if (nextToken) {
                this.textEdits.push({
                    range: {
                        start: range.end,
                        end: { line: nextToken.line - 1, character: nextToken.charPositionInLine }
                    },
                    newText: ' '.repeat(Math.max(1, alignCharacter))
                });
            }
        }
    }
    visitStatement(ctx) {
        if (ctx._start.tokenIndex > 0) {
            this.updateAfterPreviousTokenByContext(ctx, this.newline.repeat(2));
        }
        else {
            this.textEdits.push({
                range: {
                    start: {
                        line: 0,
                        character: 0
                    },
                    end: (0, protocol_translation_1.rangeFromContext)(ctx).start
                },
                newText: ''
            });
        }
        super.visitChildren(ctx);
    }
    visitStatementEnd(ctx) {
        this.updateAfterPreviousTokenByContext(ctx, this.newline);
    }
    visitBeginStatementSet(ctx) {
        this.updateBeforeNextTokenByNode(ctx.BEGIN(), ' ');
        this.updateBeforeNextTokenByNode(ctx.STATEMENT(), ' ');
    }
    visitExecuteStatementSet(ctx) {
        this.updateBeforeNextTokenByNode(ctx.EXECUTE(), ' ');
        this.updateBeforeNextTokenByNode(ctx.STATEMENT(), ' ');
        this.updateBeforeNextTokenByNode(ctx.SET(), this.newline);
    }
    visitCreateFunction(ctx) {
        this.updateAfterPreviousTokenByNode(ctx.AS(), this.newline);
        super.visitChildren(ctx);
    }
    visitCreateView(ctx) {
        if (ctx.comment()) {
            this.updateAfterPreviousTokenByContext(ctx.comment(), this.newline);
        }
        this.updateAfterPreviousTokenByNode(ctx.AS(), this.newline);
        this.updateBeforeNextTokenByNode(ctx.AS(), ' ');
        super.visitChildren(ctx);
    }
    visitCreateTable(ctx) {
        if (ctx.defineValues()) {
            this.updateAfterPreviousTokenByContext(ctx.defineValues(), this.newline);
        }
        if (ctx.comment()) {
            this.updateAfterPreviousTokenByContext(ctx.comment(), this.newline);
        }
        if (ctx.likeTable()) {
            this.updateAfterPreviousTokenByContext(ctx.likeTable(), this.newline);
        }
        if (ctx.asTable()) {
            this.updateAfterPreviousTokenByContext(ctx.asTable(), this.newline);
        }
        if (ctx.AS()) {
            this.updateAfterPreviousTokenByNode(ctx.AS(), this.newline);
            if (ctx.query()) {
                this.updateAfterPreviousTokenByContext(ctx.query(), this.newline);
            }
        }
        super.visitChildren(ctx);
    }
    visitCreateMaterializedTable(ctx) {
        this.addIndent();
        if (ctx.tableConstraint()) {
            this.updateAfterPreviousTokenByContext(ctx.tableConstraint(), this.newlineIndent);
        }
        if (ctx.comment()) {
            this.updateAfterPreviousTokenByContext(ctx.comment(), this.newlineIndent);
        }
        if (ctx.partitionedBy()) {
            this.updateAfterPreviousTokenByContext(ctx.partitionedBy(), this.newlineIndent);
        }
        if (ctx.freshness()) {
            this.updateAfterPreviousTokenByContext(ctx.freshness(), this.newlineIndent);
        }
        if (ctx.refreshMode()) {
            this.updateAfterPreviousTokenByContext(ctx.refreshMode(), this.newlineIndent);
        }
        if (ctx.AS()) {
            this.updateAfterPreviousTokenByNode(ctx.AS(), this.newlineIndent);
            if (ctx.query()) {
                this.updateAfterPreviousTokenByContext(ctx.query(), this.newlineIndent);
            }
        }
        super.visitChildren(ctx);
        this.minusIndent();
    }
    visitDefineValues(ctx) {
        this.alignColumnDefinitions(ctx);
        this.updateAfterPreviousTokenByNode(ctx.R_PARENTHESIS(), this.newlineIndent);
        this.addIndent();
        this.updateBeforeNextTokenByNode(ctx.L_PARENTHESIS(), this.newlineIndent);
        ctx.COMMA().forEach(comma => {
            this.updateAfterPreviousTokenByNode(comma, this.newlineIndent);
            this.updateBeforeNextTokenByNode(comma, '');
        });
        super.visitChildren(ctx);
        this.minusIndent();
    }
    visitColumnDefinition(ctx) {
        if (ctx.METADATA()) {
            if (ctx.FROM() || ctx.name() || ctx.VIRTUAL()) {
                this.updateBeforeNextTokenByNode(ctx.METADATA(), ' ');
            }
        }
        if (ctx.AS()) {
            this.updateBeforeNextTokenByNode(ctx.AS(), ' ');
        }
        if (ctx.comment()) {
            this.updateAfterPreviousTokenByContext(ctx.comment(), ' ');
        }
        super.visitChildren(ctx);
    }
    visitWatermarkDefinition(ctx) {
        this.updateBeforeNextTokenByNode(ctx.WATERMARK(), ' ');
        this.updateBeforeNextTokenByNode(ctx.FOR(), ' ');
        this.updateBeforeNextTokenByNode(ctx.AS(), ' ');
        super.visitChildren(ctx);
    }
    visitTableConstraint(ctx) {
        if (ctx.CONSTRAINT()) {
            this.updateBeforeNextTokenByNode(ctx.CONSTRAINT(), ' ');
        }
        if (ctx.UNIQUE()) {
            this.updateBeforeNextTokenByNode(ctx.UNIQUE(), ' ');
        }
        if (ctx.PRIMARY()) {
            this.updateBeforeNextTokenByNode(ctx.PRIMARY(), ' ');
        }
        if (ctx.KEY()) {
            this.updateBeforeNextTokenByNode(ctx.KEY(), ' ');
        }
        super.visitChildren(ctx);
    }
    visitWithValues(ctx) {
        this.updateAfterPreviousTokenByNode(ctx.WITH(), this.newlineIndent);
        this.updateBeforeNextTokenByNode(ctx.WITH(), ' ');
        super.visitChildren(ctx);
    }
    visitPartitionedBy(ctx) {
        this.updateBeforeNextTokenByNode(ctx.PARTITIONED(), ' ');
        this.updateBeforeNextTokenByNode(ctx.BY(), ' ');
        super.visitChildren(ctx);
    }
    visitInsert(ctx) {
        if (ctx.partition()) {
            this.updateAfterPreviousTokenByContext(ctx.partition(), ' ');
        }
        if (ctx.columns()) {
            this.updateAfterPreviousTokenByContext(ctx.columns(), ' ');
        }
        this.updateAfterPreviousTokenByContext(ctx.query(), this.newline);
        super.visitChildren(ctx);
    }
    visitInsertMultiple(ctx) {
        ctx.insert().forEach(insert => {
            this.updateAfterPreviousTokenByContext(insert, this.newline.repeat(2));
        });
        super.visitChildren(ctx);
    }
    visitColumns(ctx) {
        if (ctx.text.length > this.maxParenthesisLength) {
            this.updateAfterPreviousTokenByNode(ctx.R_PARENTHESIS(), this.newlineIndent);
            this.addIndent();
            this.updateBeforeNextTokenByNode(ctx.L_PARENTHESIS(), this.newlineIndent);
            ctx.COMMA().forEach(comma => {
                this.updateAfterPreviousTokenByNode(comma, this.newlineIndent);
                this.updateBeforeNextTokenByNode(comma, '');
            });
            this.minusIndent();
        }
        else {
            ctx.COMMA().forEach(comma => {
                this.updateAfterPreviousTokenByNode(comma, '');
                this.updateBeforeNextTokenByNode(comma, ' ');
            });
        }
        super.visitChildren(ctx);
    }
    visitValueAssignments(ctx) {
        if (ctx.text.length > this.maxParenthesisLength) {
            this.updateAfterPreviousTokenByNode(ctx.R_PARENTHESIS(), this.newlineIndent);
            this.addIndent();
            this.updateBeforeNextTokenByNode(ctx.L_PARENTHESIS(), this.newlineIndent);
            ctx.COMMA().forEach(comma => {
                this.updateAfterPreviousTokenByNode(comma, this.newlineIndent);
                this.updateBeforeNextTokenByNode(comma, '');
            });
            this.minusIndent();
        }
        else {
            ctx.COMMA().forEach(comma => {
                this.updateAfterPreviousTokenByNode(comma, '');
                this.updateBeforeNextTokenByNode(comma, ' ');
            });
        }
        super.visitChildren(ctx);
    }
    visitValueAssignment(ctx) {
        this.updateAfterPreviousTokenByNode(ctx.EQUAL(), ' ');
        this.updateBeforeNextTokenByNode(ctx.EQUAL(), ' ');
        super.visitChildren(ctx);
    }
    visitColumnAssignments(ctx) {
        this.updateAfterPreviousTokenByNode(ctx.L_PARENTHESIS(), ' ');
        this.updateBeforeNextTokenByNode(ctx.L_PARENTHESIS(), '');
        this.updateAfterPreviousTokenByNode(ctx.R_PARENTHESIS(), '');
        ctx.COMMA().forEach(comma => {
            this.updateAfterPreviousTokenByNode(comma, '');
            this.updateBeforeNextTokenByNode(comma, ' ');
        });
    }
    visitColumnAssignment(ctx) {
        if (ctx.EQUAL()) {
            this.updateAfterPreviousTokenByNode(ctx.EQUAL(), ' ');
            this.updateBeforeNextTokenByNode(ctx.EQUAL(), ' ');
        }
        super.visitChildren(ctx);
    }
    visitColumnsWithoutParenthesis(ctx) {
        ctx.COMMA().forEach(comma => {
            this.updateAfterPreviousTokenByNode(comma, '');
            this.updateBeforeNextTokenByNode(comma, ' ');
        });
        super.visitChildren(ctx);
    }
    visitAnalyze(ctx) {
        if (ctx.partition()) {
            this.updateAfterPreviousTokenByContext(ctx.partition(), ' ');
        }
        if (ctx.columnsWithoutParenthesis()) {
            this.updateAfterPreviousTokenByContext(ctx.columnsWithoutParenthesis(), ' ');
        }
        super.visitChildren(ctx);
    }
    visitAlterFunction(ctx) {
        this.updateAfterPreviousTokenByNode(ctx.AS(), this.newline);
        super.visitChildren(ctx);
    }
    visitRenameTo(ctx) {
        this.updateAfterPreviousTokenByNode(ctx.RENAME(), this.newline);
        this.updateBeforeNextTokenByNode(ctx.RENAME(), ' ');
        this.updateBeforeNextTokenByNode(ctx.TO(), ' ');
        super.visit(ctx.alias());
    }
    visitSetValues(ctx) {
        this.updateAfterPreviousTokenByNode(ctx.SET(), this.newline);
        this.updateBeforeNextTokenByNode(ctx.SET(), ' ');
        super.visit(ctx.valueAssignments());
    }
    visitQuery(ctx) {
        if (ctx.ctes()) {
            this.updateBeforeNextTokenByContext(ctx.ctes(), this.newlineIndent);
        }
        if (ctx.queryOrderBy()) {
            this.updateAfterPreviousTokenByContext(ctx.queryOrderBy(), this.newlineIndent);
        }
        if (ctx.queryLimit()) {
            this.updateAfterPreviousTokenByContext(ctx.queryLimit(), this.newlineIndent);
        }
        if (ctx.queryFetch()) {
            this.updateAfterPreviousTokenByContext(ctx.queryFetch(), this.newlineIndent);
        }
        super.visitChildren(ctx);
    }
    visitParenthesisQuery(ctx) {
        this.updateAfterPreviousTokenByNode(ctx.R_PARENTHESIS(), this.newlineIndent);
        this.addIndent();
        this.updateBeforeNextTokenByNode(ctx.L_PARENTHESIS(), this.newlineIndent);
        super.visitChildren(ctx);
        this.minusIndent();
    }
    visitMapQuery(ctx) {
        this.updateAfterPreviousTokenByContext(ctx, this.newlineIndent);
        this.updateAfterPreviousTokenByContext(ctx.queryTerm(), this.newlineIndent);
        super.visitChildren(ctx);
    }
    visitCtes(ctx) {
        ctx.COMMA().forEach(comma => {
            this.updateAfterPreviousTokenByNode(comma, this.newlineIndent);
            this.updateBeforeNextTokenByNode(comma, '');
        });
        super.visitChildren(ctx);
    }
    visitWithItem(ctx) {
        if (ctx.AS()) {
            this.updateAfterPreviousTokenByNode(ctx.AS(), this.newlineIndent);
        }
        this.updateAfterPreviousTokenByContext(ctx.parenthesisQuery(), this.newlineIndent);
        super.visitChildren(ctx);
    }
    visitProjectItems(ctx) {
        this.addIndent();
        this.updateAfterPreviousTokenByContext(ctx, this.newlineIndent);
        ctx.COMMA().forEach(comma => {
            this.updateAfterPreviousTokenByNode(comma, this.newlineIndent);
            this.updateBeforeNextTokenByNode(comma, '');
        });
        super.visitChildren(ctx);
        this.minusIndent();
    }
    visitProjectItem(ctx) {
        if (ctx.AS()) {
            this.updateBeforeNextTokenByNode(ctx.AS(), ' ');
        }
        super.visitChildren(ctx);
    }
    visitFromTable(ctx) {
        this.updateAfterPreviousTokenByNode(ctx.FROM(), this.newlineIndent);
        this.updateAfterPreviousTokenByContext(ctx.tableExpression(), ' ');
        super.visit(ctx.tableExpression());
    }
    visitWhere(ctx) {
        this.updateAfterPreviousTokenByNode(ctx.WHERE(), this.newlineIndent);
        this.addIndent();
        this.updateAfterPreviousTokenByContext(ctx.booleanExpr(), ' ');
        super.visit(ctx.booleanExpr());
        this.minusIndent();
    }
    visitGroupBy(ctx) {
        this.updateAfterPreviousTokenByNode(ctx.GROUP(), this.newlineIndent);
        this.addIndent();
        this.updateAfterPreviousTokenByNode(ctx.BY(), ' ');
        this.updateAfterPreviousTokenByContext(ctx.groupItems(), this.newlineIndent);
        super.visit(ctx.groupItems());
        this.minusIndent();
    }
    visitHaving(ctx) {
        this.updateAfterPreviousTokenByNode(ctx.HAVING(), this.newlineIndent);
        this.addIndent();
        this.updateAfterPreviousTokenByContext(ctx.booleanExpr(), ' ');
        super.visit(ctx.booleanExpr());
        this.minusIndent();
    }
    visitWindowSelect(ctx) {
        this.updateAfterPreviousTokenByNode(ctx.WINDOW(), this.newlineIndent);
        this.addIndent();
        ctx.COMMA().forEach(comma => {
            this.updateAfterPreviousTokenByNode(comma, this.newlineIndent);
            this.updateBeforeNextTokenByNode(comma, '');
        });
        super.visitChildren(ctx);
        this.minusIndent();
    }
    visitCommaTable(ctx) {
        this.addIndent();
        this.updateAfterPreviousTokenByNode(ctx.COMMA(), this.newlineIndent);
        this.updateBeforeNextTokenByNode(ctx.COMMA(), '');
        this.minusIndent();
        super.visitChildren(ctx);
    }
    visitMapTable(ctx) {
        this.addIndent();
        this.updateAfterPreviousTokenByContext(ctx, this.newlineIndent);
        this.updateAfterPreviousTokenByContext(ctx.tableReference(), ' ');
        if (ctx.joinCondition()) {
            this.addIndent();
            this.updateAfterPreviousTokenByContext(ctx.joinCondition(), this.newlineIndent);
            super.visit(ctx.joinCondition());
            this.minusIndent();
        }
        this.minusIndent();
        super.visit(ctx.tableReference());
    }
    visitGroupItems(ctx) {
        ctx.COMMA().forEach(comma => {
            this.updateAfterPreviousTokenByNode(comma, this.newlineIndent);
            this.updateBeforeNextTokenByNode(comma, '');
        });
        super.visitChildren(ctx);
    }
    visitParenthesisGroupItems(ctx) {
        this.updateAfterPreviousTokenByNode(ctx.R_PARENTHESIS(), this.newlineIndent);
        this.addIndent();
        this.updateBeforeNextTokenByNode(ctx.L_PARENTHESIS(), this.newlineIndent);
        super.visit(ctx.groupItems());
        this.minusIndent();
    }
    visitTableReference(ctx) {
        if (ctx.AS()) {
            this.updateAfterPreviousTokenByNode(ctx.AS(), ' ');
            this.updateBeforeNextTokenByNode(ctx.AS(), ' ');
        }
        super.visitChildren(ctx);
    }
    visitTableSource(ctx) {
        if (ctx.L_PARENTHESIS() && ctx.R_PARENTHESIS()) {
            if (ctx.UNNEST()) {
                this.updateBeforeNextTokenByNode(ctx.L_PARENTHESIS(), '');
                this.updateBeforeNextTokenByNode(ctx.L_PARENTHESIS(), '');
                this.updateAfterPreviousTokenByNode(ctx.R_PARENTHESIS(), '');
            }
            else if (ctx.TABLE()) {
                this.updateBeforeNextTokenByNode(ctx.L_PARENTHESIS(), '');
                this.updateAfterPreviousTokenByNode(ctx.R_PARENTHESIS(), '');
                this.updateBeforeNextTokenByNode(ctx.TABLE(), ' ');
            }
            else {
                this.updateAfterPreviousTokenByNode(ctx.R_PARENTHESIS(), this.newlineIndent);
                this.addIndent();
                this.updateBeforeNextTokenByNode(ctx.L_PARENTHESIS(), this.newlineIndent);
            }
        }
        super.visitChildren(ctx);
    }
    visitTableLateral(ctx) {
        this.updateBeforeNextTokenByNode(ctx.L_PARENTHESIS(), '');
        this.updateBeforeNextTokenByNode(ctx.L_PARENTHESIS(), '');
        this.updateAfterPreviousTokenByNode(ctx.R_PARENTHESIS(), '');
        if (ctx.SPECIFIC()) {
            this.updateBeforeNextTokenByNode(ctx.SPECIFIC(), ' ');
        }
        super.visitChildren(ctx);
    }
    visitSqlTypeName(ctx) {
        const typeWithParam = ctx.char() ??
            ctx.varchar() ??
            ctx.BINARY() ??
            ctx.varbinary() ??
            ctx.TIME() ??
            ctx.decimal() ??
            ctx.TIMESTAMP() ??
            ctx.TIMESTAMP_LTZ() ??
            ctx.RAW();
        if (typeWithParam && ctx.L_PARENTHESIS() && ctx.R_PARENTHESIS()) {
            this.updateAfterPreviousTokenByNode(ctx.L_PARENTHESIS(), '');
            this.updateBeforeNextTokenByNode(ctx.L_PARENTHESIS(), '');
            this.updateAfterPreviousTokenByNode(ctx.R_PARENTHESIS(), '');
            if (ctx.COMMA()) {
                this.updateAfterPreviousTokenByNode(ctx.COMMA(), '');
                this.updateBeforeNextTokenByNode(ctx.COMMA(), ' ');
            }
        }
        if ((ctx.ARRAY() || ctx.MULTISET()) && ctx.LESS() && ctx.GREATER()) {
            this.updateAfterPreviousTokenByNode(ctx.LESS(), '');
            this.updateBeforeNextTokenByNode(ctx.LESS(), '');
            this.updateAfterPreviousTokenByNode(ctx.GREATER(), '');
        }
        if (ctx.MAP() && ctx.LESS() && ctx.GREATER() && ctx.COMMA()) {
            this.updateAfterPreviousTokenByNode(ctx.LESS(), '');
            this.updateBeforeNextTokenByNode(ctx.LESS(), '');
            this.updateAfterPreviousTokenByNode(ctx.GREATER(), '');
            this.updateAfterPreviousTokenByNode(ctx.COMMA(), '');
            this.updateBeforeNextTokenByNode(ctx.COMMA(), ' ');
        }
        super.visitChildren(ctx);
    }
    visitRowType(ctx) {
        const left = ctx.LESS() ?? ctx.L_PARENTHESIS();
        const right = ctx.GREATER() ?? ctx.R_PARENTHESIS();
        const extraLength = ctx.ROW().text.length + ctx.COMMA.length;
        if (ctx.text.length - extraLength > this.maxParenthesisLength) {
            if (right) {
                this.updateAfterPreviousTokenByNode(right, this.newlineIndent);
            }
            this.addIndent();
            if (left) {
                this.updateAfterPreviousTokenByNode(left, '');
                this.updateBeforeNextTokenByNode(left, this.newlineIndent);
            }
            ctx.COMMA().forEach(comma => {
                this.updateAfterPreviousTokenByNode(comma, this.newlineIndent);
                this.updateBeforeNextTokenByNode(comma, '');
            });
            super.visitChildren(ctx);
            this.minusIndent();
        }
        else {
            if (left) {
                this.updateAfterPreviousTokenByNode(left, '');
                this.updateBeforeNextTokenByNode(left, '');
            }
            if (right) {
                this.updateAfterPreviousTokenByNode(right, '');
            }
            ctx.COMMA().forEach(comma => {
                this.updateAfterPreviousTokenByNode(comma, '');
                this.updateBeforeNextTokenByNode(comma, ' ');
            });
            super.visitChildren(ctx);
        }
        ctx.type().forEach(type => {
            this.updateAfterPreviousTokenByContext(type, ' ');
        });
    }
    visitFunctionCall(ctx) {
        this.updateAfterPreviousTokenByNode(ctx.L_PARENTHESIS(), '');
        let extraLength = 0;
        if (ctx.setQuantifier()) {
            extraLength += ctx.setQuantifier().text.length;
            this.updateBeforeNextTokenByContext(ctx.setQuantifier(), ' ');
        }
        if (ctx.aggregateNulls()) {
            extraLength += ctx.aggregateNulls().text.length;
            this.updateAfterPreviousTokenByContext(ctx.aggregateNulls(), this.newlineIndent);
        }
        if (ctx.aggregateWithin()) {
            extraLength += ctx.aggregateWithin().text.length;
            this.updateAfterPreviousTokenByContext(ctx.aggregateWithin(), this.newlineIndent);
        }
        if (ctx.aggregateFilter()) {
            extraLength += ctx.aggregateFilter().text.length;
            this.updateAfterPreviousTokenByContext(ctx.aggregateFilter(), this.newlineIndent);
        }
        if (ctx.overWindow()) {
            extraLength += ctx.overWindow().text.length;
            this.updateAfterPreviousTokenByContext(ctx.overWindow(), this.newlineIndent);
        }
        if (ctx.text.length - extraLength > this.maxParenthesisLength) {
            this.updateAfterPreviousTokenByNode(ctx.R_PARENTHESIS(), this.newlineIndent);
            this.addIndent();
            this.updateBeforeNextTokenByNode(ctx.L_PARENTHESIS(), this.newlineIndent);
            ctx.COMMA().forEach(comma => {
                this.updateAfterPreviousTokenByNode(comma, this.newlineIndent);
                this.updateBeforeNextTokenByNode(comma, '');
            });
            super.visitChildren(ctx);
            this.minusIndent();
        }
        else {
            this.updateBeforeNextTokenByNode(ctx.L_PARENTHESIS(), '');
            this.updateAfterPreviousTokenByNode(ctx.R_PARENTHESIS(), '');
            ctx.COMMA().forEach(comma => {
                this.updateAfterPreviousTokenByNode(comma, '');
                this.updateBeforeNextTokenByNode(comma, ' ');
            });
            super.visitChildren(ctx);
        }
    }
    visitJsonExistsFunction(ctx) {
        this.updateAfterPreviousTokenByNode(ctx.L_PARENTHESIS(), '');
        this.updateBeforeNextTokenByNode(ctx.L_PARENTHESIS(), '');
        this.updateAfterPreviousTokenByNode(ctx.R_PARENTHESIS(), '');
        this.updateAfterPreviousTokenByNode(ctx.COMMA(), '');
        this.updateBeforeNextTokenByNode(ctx.COMMA(), ' ');
        super.visitChildren(ctx);
    }
    visitJsonValueFunction(ctx) {
        this.updateAfterPreviousTokenByNode(ctx.L_PARENTHESIS(), '');
        this.updateBeforeNextTokenByNode(ctx.L_PARENTHESIS(), '');
        this.updateAfterPreviousTokenByNode(ctx.R_PARENTHESIS(), '');
        this.updateAfterPreviousTokenByNode(ctx.COMMA(), '');
        this.updateBeforeNextTokenByNode(ctx.COMMA(), ' ');
        if (ctx.RETURNING()) {
            this.updateAfterPreviousTokenByNode(ctx.RETURNING(), ' ');
            this.updateBeforeNextTokenByNode(ctx.RETURNING(), ' ');
        }
        super.visitChildren(ctx);
    }
    visitJsonQueryFunction(ctx) {
        this.updateAfterPreviousTokenByNode(ctx.L_PARENTHESIS(), '');
        this.updateBeforeNextTokenByNode(ctx.L_PARENTHESIS(), '');
        this.updateAfterPreviousTokenByNode(ctx.R_PARENTHESIS(), '');
        this.updateAfterPreviousTokenByNode(ctx.COMMA(), '');
        this.updateBeforeNextTokenByNode(ctx.COMMA(), ' ');
        super.visitChildren(ctx);
    }
    visitJsonObjectFunction(ctx) {
        this.updateAfterPreviousTokenByNode(ctx.L_PARENTHESIS(), '');
        this.updateBeforeNextTokenByNode(ctx.L_PARENTHESIS(), '');
        this.updateAfterPreviousTokenByNode(ctx.R_PARENTHESIS(), '');
        ctx.COMMA().forEach(comma => {
            this.updateAfterPreviousTokenByNode(comma, '');
            this.updateBeforeNextTokenByNode(comma, ' ');
        });
        if (ctx.nullBehavior()) {
            this.updateAfterPreviousTokenByContext(ctx.nullBehavior(), ' ');
        }
        super.visitChildren(ctx);
    }
    visitJsonObjectAggFunction(ctx) {
        this.updateAfterPreviousTokenByNode(ctx.L_PARENTHESIS(), '');
        this.updateBeforeNextTokenByNode(ctx.L_PARENTHESIS(), '');
        this.updateAfterPreviousTokenByNode(ctx.R_PARENTHESIS(), '');
        if (ctx.nullBehavior()) {
            this.updateAfterPreviousTokenByContext(ctx.nullBehavior(), ' ');
        }
        super.visitChildren(ctx);
    }
    visitJsonArrayFunction(ctx) {
        this.updateAfterPreviousTokenByNode(ctx.L_PARENTHESIS(), '');
        this.updateBeforeNextTokenByNode(ctx.L_PARENTHESIS(), '');
        this.updateAfterPreviousTokenByNode(ctx.R_PARENTHESIS(), '');
        ctx.COMMA().forEach(comma => {
            this.updateAfterPreviousTokenByNode(comma, '');
            this.updateBeforeNextTokenByNode(comma, ' ');
        });
        if (ctx.nullBehavior()) {
            this.updateAfterPreviousTokenByContext(ctx.nullBehavior(), ' ');
        }
        super.visitChildren(ctx);
    }
    visitJsonArrayAggFunction(ctx) {
        this.updateAfterPreviousTokenByNode(ctx.L_PARENTHESIS(), '');
        this.updateBeforeNextTokenByNode(ctx.L_PARENTHESIS(), '');
        this.updateAfterPreviousTokenByNode(ctx.R_PARENTHESIS(), '');
        if (ctx.orderBy()) {
            this.updateAfterPreviousTokenByContext(ctx.orderBy(), ' ');
        }
        if (ctx.nullBehavior()) {
            this.updateAfterPreviousTokenByContext(ctx.nullBehavior(), ' ');
        }
        super.visitChildren(ctx);
    }
    visitExpressions(ctx) {
        ctx.COMMA().forEach(comma => {
            this.updateAfterPreviousTokenByNode(comma, '');
            this.updateBeforeNextTokenByNode(comma, ' ');
        });
        super.visitChildren(ctx);
    }
    visitPrimaryExpr(ctx) {
        const functionName = ctx.CAST() ??
            ctx.TRY_CAST() ??
            ctx.TUMBLE() ??
            ctx.HOP() ??
            ctx.CUMULATE() ??
            ctx.SESSION() ??
            ctx.EXTRACT() ??
            ctx.FLOOR() ??
            ctx.CEIL() ??
            ctx.POSITION() ??
            ctx.TRIM() ??
            ctx.OVERLAY() ??
            ctx.SUBSTRING() ??
            ctx.SUBSTR();
        if (functionName && ctx.L_PARENTHESIS() && ctx.R_PARENTHESIS()) {
            this.updateAfterPreviousTokenByNode(ctx.L_PARENTHESIS(), '');
            this.updateBeforeNextTokenByNode(ctx.L_PARENTHESIS(), '');
            this.updateAfterPreviousTokenByNode(ctx.R_PARENTHESIS(), '');
        }
        if (ctx.L_BRACKET() && ctx.R_BRACKET()) {
            this.updateAfterPreviousTokenByNode(ctx.L_BRACKET(), '');
            this.updateBeforeNextTokenByNode(ctx.L_BRACKET(), '');
            this.updateAfterPreviousTokenByNode(ctx.R_BRACKET(), '');
        }
        super.visitChildren(ctx);
    }
    visitArithmeticUnary(ctx) {
        this.updateAfterPreviousTokenByContext(ctx.valueExpr(), '');
        super.visitChildren(ctx);
    }
    visitArithmeticBinary(ctx) {
        this.updateBeforeNextTokenByContext(ctx._left, ' ');
        this.updateAfterPreviousTokenByContext(ctx._right, ' ');
        super.visitChildren(ctx);
    }
    visitComparison(ctx) {
        this.updateBeforeNextTokenByContext(ctx._left, ' ');
        this.updateAfterPreviousTokenByContext(ctx._right, ' ');
        super.visitChildren(ctx);
    }
    visitWhenExpr(ctx) {
        this.addIndent();
        this.updateAfterPreviousTokenByContext(ctx, this.newlineIndent);
        super.visitChildren(ctx);
        this.minusIndent();
    }
    visitThenExpr(ctx) {
        this.updateAfterPreviousTokenByContext(ctx, ' ');
        super.visitChildren(ctx);
    }
    visitElseExpr(ctx) {
        this.addIndent();
        this.updateAfterPreviousTokenByNode(ctx.ELSE(), this.newlineIndent);
        super.visitChildren(ctx);
        this.minusIndent();
    }
    visitCaseEnd(ctx) {
        this.updateAfterPreviousTokenByNode(ctx.END(), this.newlineIndent);
    }
    visitLogicalNot(ctx) {
        this.updateBeforeNextTokenByNode(ctx.NOT(), ' ');
        super.visit(ctx.booleanExpr());
    }
    visitExists(ctx) {
        this.updateAfterPreviousTokenByContext(ctx.parenthesisQuery(), ' ');
        super.visit(ctx.parenthesisQuery());
    }
    visitLogicalBinary(ctx) {
        this.updateAfterPreviousTokenByContext(ctx._operator, this.newlineIndent);
        this.updateAfterPreviousTokenByContext(ctx._right, ' ');
        super.visitChildren(ctx);
    }
    visitWindowSpec(ctx) {
        this.addIndent();
        if (ctx.name()) {
            this.updateAfterPreviousTokenByContext(ctx.name(), this.newlineIndent);
        }
        if (ctx.partitionBy()) {
            this.updateAfterPreviousTokenByContext(ctx.partitionBy(), this.newlineIndent);
        }
        if (ctx.orderBy()) {
            this.updateAfterPreviousTokenByContext(ctx.orderBy(), this.newlineIndent);
        }
        if (ctx.rangeOrRows()) {
            this.updateAfterPreviousTokenByContext(ctx.rangeOrRows(), this.newlineIndent);
        }
        super.visitChildren(ctx);
        this.minusIndent();
        this.updateAfterPreviousTokenByNode(ctx.R_PARENTHESIS(), this.newlineIndent);
    }
    visitMatchRecognize(ctx) {
        this.addIndent();
        if (ctx.partitionBy()) {
            this.updateAfterPreviousTokenByContext(ctx.partitionBy(), this.newlineIndent);
        }
        if (ctx.orderBy()) {
            this.updateAfterPreviousTokenByContext(ctx.orderBy(), this.newlineIndent);
        }
        if (ctx.measures()) {
            this.updateAfterPreviousTokenByContext(ctx.measures(), this.newlineIndent);
        }
        if (ctx.perMatch()) {
            this.updateAfterPreviousTokenByContext(ctx.perMatch(), this.newlineIndent);
        }
        if (ctx.afterMatch()) {
            this.updateAfterPreviousTokenByContext(ctx.afterMatch(), this.newlineIndent);
        }
        if (ctx.patternMatch()) {
            this.updateAfterPreviousTokenByContext(ctx.patternMatch(), this.newlineIndent);
        }
        if (ctx.withinMatch()) {
            this.updateAfterPreviousTokenByContext(ctx.withinMatch(), this.newlineIndent);
        }
        if (ctx.subsetMatch()) {
            this.updateAfterPreviousTokenByContext(ctx.subsetMatch(), this.newlineIndent);
        }
        if (ctx.defineMatch()) {
            this.updateAfterPreviousTokenByContext(ctx.defineMatch(), this.newlineIndent);
        }
        super.visitChildren(ctx);
        this.minusIndent();
        this.updateAfterPreviousTokenByNode(ctx.R_PARENTHESIS(), this.newlineIndent);
    }
    visitCharsetLiteral(ctx) {
        this.updateBeforeNextTokenByContext(ctx.charsetIntroducer(), '');
        super.visitChildren(ctx);
    }
    visitDereference(ctx) {
        this.updateAfterPreviousTokenByNode(ctx.DOT(), '');
        this.updateBeforeNextTokenByNode(ctx.DOT(), '');
        super.visitChildren(ctx);
    }
    visitStar(ctx) {
        if (ctx.DOT()) {
            this.updateAfterPreviousTokenByNode(ctx.DOT(), '');
            this.updateBeforeNextTokenByNode(ctx.DOT(), '');
        }
        super.visitChildren(ctx);
    }
}
exports.SpaceReplacerFormatVisitor = SpaceReplacerFormatVisitor;
