"use strict";
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.recreateQueryStringFromFormattedSyntaxTrees = void 0;
const types_1 = require("./types");
const utils_1 = require("./utils");
const recreateQueryStringFromFormattedSyntaxTree = (syntaxTree) => {
    if (syntaxTree.type === types_1.TokenType.NonGremlinCode) {
        return syntaxTree.code;
    }
    if (syntaxTree.type === types_1.TokenType.Traversal) {
        return syntaxTree.stepGroups
            .map((stepGroup, index) => {
            const firstStep = stepGroup.steps[0];
            const indentationAmount = index === 0 || firstStep.type === types_1.TokenType.NonGremlinCode ? 0 : firstStep.localIndentation;
            const indentation = (0, utils_1.spaces)(indentationAmount);
            return indentation + stepGroup.steps.map(recreateQueryStringFromFormattedSyntaxTree).join('.');
        })
            .join('\n');
    }
    if (syntaxTree.type === types_1.TokenType.Method) {
        const methodOpeningParenthesis = (syntaxTree.shouldStartWithDot ? '.' : '') + recreateQueryStringFromFormattedSyntaxTree(syntaxTree.method) + '(';
        const indentation = (0, utils_1.spaces)(syntaxTree.horizontalPosition + methodOpeningParenthesis.length);
        return (methodOpeningParenthesis +
            syntaxTree.argumentGroups
                .map((args) => args.map(recreateQueryStringFromFormattedSyntaxTree).join(', '))
                .join(',\n' + indentation) +
            ')' +
            (syntaxTree.shouldEndWithDot ? '.' : ''));
    }
    if (syntaxTree.type === types_1.TokenType.Closure) {
        return ((syntaxTree.shouldStartWithDot ? '.' : '') +
            recreateQueryStringFromFormattedSyntaxTree(syntaxTree.method) +
            '{' +
            syntaxTree.closureCodeBlock
                .map(({ lineOfCode, localIndentation }, i) => `${(0, utils_1.spaces)(localIndentation)}${lineOfCode}`)
                .join('\n') +
            '}' +
            (syntaxTree.shouldEndWithDot ? '.' : ''));
    }
    if (syntaxTree.type === types_1.TokenType.String) {
        return syntaxTree.string;
    }
    if (syntaxTree.type === types_1.TokenType.Word) {
        return (syntaxTree.shouldStartWithDot ? '.' : '') + syntaxTree.word + (syntaxTree.shouldEndWithDot ? '.' : '');
    }
    // The following line is just here to convince TypeScript that the return type
    // is string and not string | undefined.
    return '';
};
const withIndentationIfNotEmpty = (indentation) => (lineOfCode) => {
    if (!lineOfCode)
        return lineOfCode;
    return (0, utils_1.spaces)(indentation) + lineOfCode;
};
const lineIsEmpty = (lineOfCode) => {
    return lineOfCode.split('').every((0, utils_1.eq)(' '));
};
const removeIndentationFromEmptyLines = (lineOfCode) => {
    if (lineIsEmpty(lineOfCode))
        return '';
    return lineOfCode;
};
const recreateQueryStringFromFormattedSyntaxTrees = ({ globalIndentation }) => (syntaxTrees) => {
    return syntaxTrees
        .map(recreateQueryStringFromFormattedSyntaxTree)
        .join('')
        .split('\n')
        .map(withIndentationIfNotEmpty(globalIndentation))
        .map(removeIndentationFromEmptyLines)
        .join('\n');
};
exports.recreateQueryStringFromFormattedSyntaxTrees = recreateQueryStringFromFormattedSyntaxTrees;
