"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const Command_1 = require("../minecraft/Command");
const Carto_1 = require("./Carto");
class CommandRunner {
    static async runCustomTool(carto, commandNumber) {
        if (carto.activeMinecraftState !== Carto_1.CartoMinecraftState.started) {
            // alert("Cannot run command " + commandNumber + "; not connected to Minecraft.");
        }
        const cartoCommand = carto.getCustomTool(commandNumber - 1);
        let commandName = commandNumber + "";
        if (cartoCommand.name !== undefined && cartoCommand.name.length > 0) {
            commandName = cartoCommand.name;
        }
        const operId = await carto.notifyOperationStarted("Running command " + commandName);
        if (cartoCommand.text !== undefined) {
            await CommandRunner.runCommandText(carto, cartoCommand.text);
        }
        else {
            // alert("This command is not defined, yet.");
        }
        await carto.notifyOperationEnded(operId, "Command " + commandName + " complete.");
    }
    static async runCommandText(carto, commandText) {
        const commandItems = commandText.split("\n");
        await this.runCommandList(carto, commandItems);
    }
    static async runCommandList(carto, commandItems) {
        const resultItems = [];
        let lastStoredBlockX = -1;
        let lastStoredBlockY = -1;
        let lastStoredBlockZ = -1;
        let firstRelativeX;
        let firstRelativeY;
        let firstRelativeZ;
        let setFirstPosition = false;
        for (let i = 0; i < commandItems.length; i++) {
            let commandText = commandItems[i].trim();
            if (commandText.length > 4 && !commandText.startsWith("#")) {
                if (!commandText.startsWith("/")) {
                    commandText = "/" + commandText;
                }
                const commandStart = new Date();
                const command = new Command_1.default(commandText);
                // fix up a command to use absolute positions based on the position of our first command.
                if (command.hasRelativeOrLocalCoordinates &&
                    setFirstPosition &&
                    firstRelativeX !== undefined &&
                    firstRelativeY !== undefined &&
                    firstRelativeZ !== undefined) {
                    command.absolutizeCoordinates(lastStoredBlockX - firstRelativeX, lastStoredBlockY - firstRelativeY, lastStoredBlockZ - firstRelativeZ);
                }
                commandText = command.toString();
                await carto.notifyStatusUpdate("Running command '" + commandText + "'");
                const commandResult = await carto.runCommand(commandText);
                const result = commandResult?.data;
                let resultData;
                if (result !== undefined && result !== null && result.indexOf("{") >= 0) {
                    try {
                        resultData = JSON.parse(result);
                    }
                    catch (e) { }
                    if (resultData !== undefined) {
                        // store a position so that we can absolutize future commands
                        if (resultData.position !== undefined && !setFirstPosition) {
                            lastStoredBlockX = resultData.position.x;
                            lastStoredBlockY = resultData.position.y;
                            lastStoredBlockZ = resultData.position.z;
                            const argX = command.firstX;
                            if (argX !== undefined) {
                                firstRelativeX = argX.position;
                            }
                            const argY = command.firstY;
                            if (argY !== undefined) {
                                firstRelativeY = argY.position;
                            }
                            const argZ = command.firstZ;
                            if (argZ !== undefined) {
                                firstRelativeZ = argZ.position;
                            }
                            setFirstPosition = true;
                        }
                    }
                }
                const resultItem = {
                    lineNumber: i,
                    dateTime: commandStart,
                    response: resultData,
                };
                resultItems.push(resultItem);
            }
        }
        return resultItems;
    }
}
exports.default = CommandRunner;

//# sourceMappingURL=../maps/app/CommandRunner.js.map
