"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const Log_1 = require("../core/Log");
const ste_events_1 = require("ste-events");
const Utilities_1 = require("../core/Utilities");
class VsCodeTasksDefinition {
    _file;
    _id;
    _version;
    _isLoaded = false;
    definition;
    _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 VsCodeTasksDefinition();
            dt.file = file;
            file.manager = dt;
        }
        if (file.manager !== undefined && file.manager instanceof VsCodeTasksDefinition) {
            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 launchJsonString = JSON.stringify(this.definition, null, 2);
        this._file.setContent(launchJsonString);
    }
    async save() {
        if (this._file === undefined) {
            return;
        }
        this.persist();
        await this._file.saveContent(false);
    }
    async hasMinContent() {
        await this.load();
        if (!this.definition || !this.definition.tasks) {
            return false;
        }
        for (const task of this.definition.tasks) {
            if (task.dependsOn && task.label === "build") {
                for (const dependsOn of task.dependsOn) {
                    const strTask = Utilities_1.default.replaceAll(dependsOn.toLowerCase(), " ", "");
                    if (strTask === "minecraft:deploy") {
                        return true;
                    }
                }
            }
        }
        return false;
    }
    async ensureMinContent() {
        const hasTasks = await this.hasMinContent();
        if (hasTasks) {
            return true;
        }
        if (!this.definition) {
            this.definition = {};
        }
        if (!this.definition.version) {
            this.definition.version = "2.0.0";
        }
        if (!this.definition.tasks) {
            this.definition.tasks = [];
        }
        let hasBuildDependsOn = false;
        for (const task of this.definition.tasks) {
            if (task.label === "build") {
                if (!task.dependsOn) {
                    task.dependsOn = [];
                }
                for (const dependsOn of task.dependsOn) {
                    const strTask = Utilities_1.default.replaceAll(dependsOn.toLowerCase(), " ", "");
                    if (strTask === "minecraft:deploy") {
                        hasBuildDependsOn = true;
                    }
                }
                if (!hasBuildDependsOn) {
                    task.dependsOn.push("minecraft: deploy");
                    hasBuildDependsOn = true;
                }
            }
        }
        if (!hasBuildDependsOn) {
            this.definition.tasks.push({
                label: "build",
                dependsOrder: "sequence",
                dependsOn: ["minecraft: deploy"],
            });
            return true;
        }
        return hasBuildDependsOn;
    }
    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 = VsCodeTasksDefinition;

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