"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const Utilities_1 = require("../../core/Utilities");
class DocumentedCommand {
    _id;
    _isLoaded = false;
    commandSet;
    infoJsonFiles;
    commandDefinition;
    get isLoaded() {
        return this._isLoaded;
    }
    get id() {
        return this._id;
    }
    set id(newId) {
        this._id = newId;
    }
    get docCommand() {
        return this.commandDefinition;
    }
    constructor(commandSet, docCommand) {
        this.commandDefinition = docCommand;
        this.commandSet = commandSet;
        this._id = docCommand.name;
    }
    async load() {
        await this.ensureInfoJson(this.commandSet.docFolder);
        this._isLoaded = true;
    }
    getOverloads() {
        return this.docCommand.overloads;
    }
    getArguments() {
        const overloadArgs = {};
        for (const overload of this.docCommand.overloads) {
            for (const param of overload.params) {
                overloadArgs[param.name] = param;
            }
        }
        return overloadArgs;
    }
    getOverload(index) {
        const overloads = this.docCommand.overloads;
        if (overloads && overloads.length > index) {
            return overloads[index];
        }
        return undefined;
    }
    getParam(name) {
        const overloads = this.docCommand.overloads;
        if (overloads) {
            for (const overload of overloads) {
                for (const param of overload.params) {
                    if (param.name === name) {
                        return param;
                    }
                }
            }
        }
        return undefined;
    }
    getAlias(name) {
        const aliases = this.docCommand.aliases;
        if (aliases) {
            for (const alias of aliases) {
                if (alias.alias === name) {
                    return alias;
                }
            }
        }
        return undefined;
    }
    async persist() {
        if (this.infoJsonFiles) {
            for (const fileName in this.infoJsonFiles) {
                const infoJsonFile = this.infoJsonFiles[fileName];
                await infoJsonFile.saveContent();
            }
        }
    }
    async ensureInfoJson(docsFolder) {
        if (!this._id) {
            return;
        }
        const rootFolder = this.commandSet.ensureDocFolder(docsFolder);
        if (!rootFolder) {
            return;
        }
        const commandsFolder = rootFolder.ensureFolder("commands");
        if (!commandsFolder) {
            return;
        }
        this.infoJsonFiles = {};
        const commandFolder = commandsFolder.ensureFolder(this._id);
        const rootFile = commandFolder.ensureFile("info.json");
        await rootFile.loadContent();
        this.infoJsonFiles["_command"] = rootFile;
        const paramTypeNames = [];
        for (const docOverload of this.commandDefinition.overloads) {
            for (const docParam of docOverload.params) {
                if (docParam.type && docParam.type.name && !paramTypeNames.includes(docParam.type.name)) {
                    paramTypeNames.push(docParam.type.name);
                }
            }
        }
        for (const paramTypeName of paramTypeNames) {
            const enumVal = this.commandSet.getEnum(paramTypeName);
            if (enumVal && !this.commandSet.isEnumUsedInMultipleCommands(enumVal)) {
                const enumFolder = commandFolder.ensureFolder("command_enums").ensureFolder(enumVal.name);
                const exists = await enumFolder.exists();
                if (exists) {
                    const enumFile = enumFolder.ensureFile("info.json");
                    await enumFile.loadContent();
                    if (Utilities_1.default.isUsableAsObjectKey(enumVal.name)) {
                        this.infoJsonFiles[enumVal.name] = enumFile;
                    }
                }
            }
        }
    }
}
exports.default = DocumentedCommand;

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