"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.
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatTraversal = void 0;
const recreateQueryOnelinerFromSyntaxTree_1 = __importDefault(require("../../recreateQueryOnelinerFromSyntaxTree"));
const types_1 = require("../../types");
const utils_1 = require("../../utils");
const layoutUtils_1 = require("../../layoutUtils");
const utils_2 = require("../utils");
const getStepGroups_1 = require("./getStepGroups");
const utils_3 = require("./getStepGroups/utils");
// Groups steps into step groups and adds a localIndentation property
const formatTraversal = (formatSyntaxTree) => (config) => (syntaxTree) => {
    const initialHorizontalPositionIndentationIncrease = syntaxTree.steps[0] && (0, utils_3.isTraversalSource)(syntaxTree.steps[0]) ? syntaxTree.initialHorizontalPosition : 0;
    if ((0, layoutUtils_1.willFitOnLine)(syntaxTree, config, config.horizontalPosition + initialHorizontalPositionIndentationIncrease)) {
        const recreatedQuery = (0, recreateQueryOnelinerFromSyntaxTree_1.default)(config, config.horizontalPosition + initialHorizontalPositionIndentationIncrease)(syntaxTree);
        return {
            type: types_1.TokenType.Traversal,
            steps: syntaxTree.steps,
            stepGroups: [
                {
                    steps: syntaxTree.steps.reduce((steps, step, stepIndex) => {
                        const formattedStep = stepIndex === 0
                            ? formatSyntaxTree(config)(step)
                            : // Since the traversal's steps will be on the same line, their horizontal position is increased by the
                                // steps's width plus the width of the dots between them
                                formatSyntaxTree((0, utils_1.pipe)(utils_2.withZeroIndentation, (0, utils_2.withIncreasedHorizontalPosition)(syntaxTree.initialHorizontalPosition +
                                    steps.map(({ width }) => width).reduce(utils_1.sum, 0) +
                                    steps.length))(config))(step);
                        return [...steps, formattedStep];
                    }, []),
                },
            ],
            initialHorizontalPosition: syntaxTree.initialHorizontalPosition,
            localIndentation: 0,
            width: recreatedQuery.trim().length,
        };
    }
    const stepGroups = (0, getStepGroups_1.getStepGroups)(formatSyntaxTree, syntaxTree.steps, config);
    const lastStepGroup = (0, utils_1.last)(stepGroups);
    const width = lastStepGroup
        ? lastStepGroup.steps.map(({ width }) => width).reduce(utils_1.sum, 0) + stepGroups.length - 1
        : 0;
    return {
        type: types_1.TokenType.Traversal,
        steps: syntaxTree.steps,
        stepGroups,
        initialHorizontalPosition: syntaxTree.initialHorizontalPosition,
        localIndentation: 0,
        width,
    };
};
exports.formatTraversal = formatTraversal;
