"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const ste_events_1 = require("ste-events");
const DocumentedCommand_1 = require("./DocumentedCommand");
const Project_1 = require("../../app/Project");
const StorageUtilities_1 = require("../../storage/StorageUtilities");
class DocumentedCommandSet {
    _file;
    _id;
    _name;
    _version;
    _isLoaded = false;
    _docFolder;
    _docCommands = {};
    commandSetDefinition;
    _onLoaded = new ste_events_1.EventDispatcher();
    get isLoaded() {
        return this._isLoaded;
    }
    get docFolder() {
        return this._docFolder;
    }
    get commands() {
        return this._docCommands;
    }
    get file() {
        return this._file;
    }
    set file(newFile) {
        this._file = newFile;
    }
    get onLoaded() {
        return this._onLoaded.asEvent();
    }
    get name() {
        if (this.commandSetDefinition) {
            return this.commandSetDefinition.name;
        }
        return this._name;
    }
    get id() {
        return this._id;
    }
    set id(newId) {
        this._id = newId;
        if (newId) {
            const underscore = newId.lastIndexOf("_");
            if (underscore >= 0 && underscore < newId.length - 2) {
                this._name = newId.substring(0, underscore);
                this._version = newId.substring(underscore + 1);
            }
            else {
                this._name = newId;
                this._version = "";
            }
        }
    }
    constructor(docFolder) {
        this._docFolder = docFolder;
    }
    getEnum(name) {
        const enums = this.commandSetDefinition?.command_enums;
        if (enums) {
            name = name.toLowerCase();
            for (const enumItem of enums) {
                if (enumItem.name.toLowerCase() === name) {
                    return enumItem;
                }
            }
        }
        return undefined;
    }
    static async ensureOnFile(file, docFolder, loadHandler) {
        let dt;
        if (file.manager === undefined) {
            dt = new DocumentedCommandSet(docFolder);
            dt.file = file;
            file.manager = dt;
        }
        if (file.manager !== undefined && file.manager instanceof DocumentedCommandSet) {
            dt = file.manager;
            if (!dt.isLoaded && loadHandler) {
                dt.onLoaded.subscribe(loadHandler);
            }
            await dt.load();
        }
        return dt;
    }
    ensureDocFolder(docsFolder) {
        if (!this.name) {
            return;
        }
        let folderName = this.name;
        folderName = folderName.toLowerCase();
        if (Project_1.remappedMinecraftScriptModules[folderName]) {
            folderName = Project_1.remappedMinecraftScriptModules[folderName];
        }
        const folders = folderName.split("/");
        let curFolder = docsFolder;
        for (let i = 0; i < folders.length; i++) {
            curFolder = curFolder.ensureFolder(folders[i]);
        }
        return curFolder;
    }
    isEnumUsedInMultipleCommands(enumInstance) {
        let commandCount = 0;
        for (const commandName in this.commands) {
            const command = this.commands[commandName];
            if (command) {
                let isInCommand = false;
                const overloads = command.getOverloads();
                for (const overload of overloads) {
                    for (const param of overload.params) {
                        if (param.type && param.type.name.toUpperCase() === enumInstance.name.toUpperCase()) {
                            isInCommand = true;
                        }
                    }
                }
                if (isInCommand) {
                    commandCount++;
                    if (commandCount > 1) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
    persist() {
        if (this._file === undefined) {
            return;
        }
        for (const docCommandName in this._docCommands) {
            const docCommand = this._docCommands[docCommandName];
            docCommand.persist();
        }
        //const cdString = JSON.stringify(this.moduleDefinition, null, 2);
        //this._behaviorPackFile.setContent(bpString);
    }
    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;
        this.commandSetDefinition = StorageUtilities_1.default.getJsonObject(this._file);
        this._docCommands = {};
        if (this.commandSetDefinition) {
            for (const docClass of this.commandSetDefinition?.commands) {
                this._docCommands[docClass.name] = new DocumentedCommand_1.default(this, docClass);
            }
        }
        this._isLoaded = true;
    }
}
exports.default = DocumentedCommandSet;

//# sourceMappingURL=../../maps/minecraft/docs/DocumentedCommandSet.js.map
