"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.
 *--------------------------------------------------------------------------------------------*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.YAMLFormatter = void 0;
const vscode_languageserver_types_1 = require("vscode-languageserver-types");
const yamlPlugin = __importStar(require("prettier/plugins/yaml"));
const estreePlugin = __importStar(require("prettier/plugins/estree"));
const standalone_1 = require("prettier/standalone");
class YAMLFormatter {
    constructor() {
        this.formatterEnabled = true;
    }
    configure(shouldFormat) {
        if (shouldFormat) {
            this.formatterEnabled = shouldFormat.format;
        }
    }
    async format(document, options = {}) {
        if (!this.formatterEnabled) {
            return [];
        }
        try {
            const text = document.getText();
            const prettierOptions = {
                parser: 'yaml',
                plugins: [yamlPlugin, estreePlugin],
                // --- FormattingOptions ---
                tabWidth: options.tabWidth || options.tabSize,
                // --- CustomFormatterOptions ---
                singleQuote: options.singleQuote,
                bracketSpacing: options.bracketSpacing,
                // 'preserve' is the default for Options.proseWrap. See also server.ts
                proseWrap: 'always' === options.proseWrap ? 'always' : 'never' === options.proseWrap ? 'never' : 'preserve',
                printWidth: options.printWidth,
                trailingComma: options.trailingComma === false ? 'none' : 'all',
            };
            const formatted = await (0, standalone_1.format)(text, prettierOptions);
            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 {
            return [];
        }
    }
}
exports.YAMLFormatter = YAMLFormatter;
//# sourceMappingURL=yamlFormatter.js.map