"use strict";
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Red Hat, Inc. All rights reserved.
 *  Copyright (c) Adam Voss. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.YAMLFormatter = void 0;
const vscode_languageserver_types_1 = require("vscode-languageserver-types");
const yaml_1 = require("yaml");
const custom_tag_provider_1 = require("../parser/custom-tag-provider");
class YAMLFormatter {
    constructor() {
        this.formatterEnabled = true;
        this.yamlVersion = '1.2';
        this.customTags = [];
    }
    configure(shouldFormat) {
        if (shouldFormat) {
            this.formatterEnabled = shouldFormat.format;
            this.yamlVersion = shouldFormat.yamlVersion;
            this.customTags = shouldFormat.customTags;
        }
    }
    async format(document, options = {}) {
        if (!this.formatterEnabled) {
            return [];
        }
        try {
            const text = document.getText();
            const doc = (0, yaml_1.parseDocument)(text, {
                version: this.yamlVersion,
                customTags: (0, custom_tag_provider_1.getCustomTags)(this.customTags),
            });
            const toStringOptions = {
                // --- FormattingOptions ---
                indent: options.tabWidth || options.tabSize || 2,
                // --- CustomFormatterOptions ---
                singleQuote: options.singleQuote,
                flowCollectionPadding: options.bracketSpacing,
                blockQuote: options.proseWrap === 'always' ? 'folded' : true,
                lineWidth: Math.max(options.printWidth || 0, 22),
                trailingComma: options.trailingComma === undefined ? true : options.trailingComma,
            };
            const formatted = doc.toString(toStringOptions);
            if (formatted === text) {
                return [];
            }
            return [vscode_languageserver_types_1.TextEdit.replace(vscode_languageserver_types_1.Range.create(vscode_languageserver_types_1.Position.create(0, 0), document.positionAt(text.length)), formatted)];
        }
        catch (error) {
            return [];
        }
    }
}
exports.YAMLFormatter = YAMLFormatter;
//# sourceMappingURL=yamlFormatter.js.map