"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.PrettierRcDefaultSettings = void 0;
const Log_1 = require("../core/Log");
const ste_events_1 = require("ste-events");
exports.PrettierRcDefaultSettings = {
    trailingComma: "es5",
    tabWidth: 2,
    semi: true,
    singleQuote: false,
    bracketSpacing: true,
    arrowParens: "always",
    printWidth: 120,
    endOfLine: "auto",
};
class PrettierRcConfig {
    constructor() {
        this._isLoaded = false;
        this._onLoaded = new ste_events_1.EventDispatcher();
    }
    get isLoaded() {
        return this._isLoaded;
    }
    get file() {
        return this._file;
    }
    set file(newFile) {
        this._file = newFile;
    }
    get onLoaded() {
        return this._onLoaded.asEvent();
    }
    get id() {
        return this._id;
    }
    set id(newId) {
        this._id = newId;
    }
    static async ensureOnFile(file, loadHandler) {
        let dt;
        if (file.manager === undefined) {
            dt = new PrettierRcConfig();
            dt.file = file;
            file.manager = dt;
        }
        if (file.manager !== undefined && file.manager instanceof PrettierRcConfig) {
            dt = file.manager;
            if (!dt.isLoaded && loadHandler) {
                dt.onLoaded.subscribe(loadHandler);
            }
            await dt.load();
            return dt;
        }
        return dt;
    }
    async persist() {
        if (this._file === undefined) {
            return;
        }
        const prettierRcJsonString = JSON.stringify(this.definition, null, 2);
        this._file.setContent(prettierRcJsonString);
    }
    async save() {
        if (this._file === undefined) {
            return;
        }
        this.persist();
        await this._file.saveContent(false);
    }
    async hasMinContent() {
        await this.load();
        if (!this.definition) {
            return false;
        }
        for (const settingName in exports.PrettierRcDefaultSettings) {
            if (!this.definition[settingName]) {
                return false;
            }
        }
        return true;
    }
    async ensureMinContent() {
        const hasSettings = await this.hasMinContent();
        if (hasSettings) {
            return true;
        }
        if (!this.definition) {
            this.definition = exports.PrettierRcDefaultSettings;
        }
        for (const settingName in exports.PrettierRcDefaultSettings) {
            if (!this.definition[settingName]) {
                this.definition[settingName] = exports.PrettierRcDefaultSettings[settingName];
            }
        }
        return this.definition;
    }
    async load() {
        if (this._file === undefined || this._isLoaded) {
            return;
        }
        await this._file.loadContent();
        if (this._file.content === null || this._file.content instanceof Uint8Array) {
            return;
        }
        this.id = this._file.name;
        try {
            const data = JSON.parse(this._file.content);
            this.definition = data;
        }
        catch (e) {
            Log_1.default.fail("Could not parse vscode launch JSON " + e);
        }
        this._isLoaded = true;
    }
}
exports.default = PrettierRcConfig;

//# sourceMappingURL=../maps/devproject/PrettierRcConfig.js.map
