"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const CommandArgument_1 = require("./CommandArgument");
class Command {
    _commandName;
    _arguments;
    constructor(commandText) {
        const firstSpace = commandText.indexOf(" ");
        this._arguments = [];
        let _coordinatePos = -1;
        if (firstSpace < 0) {
            this._commandName = commandText;
        }
        else {
            this._commandName = commandText.substring(0, firstSpace);
            const args = commandText.substring(firstSpace + 1, commandText.length).split(" ");
            for (let i = 0; i < args.length; i++) {
                const cmdArg = new CommandArgument_1.default(args[i]);
                if (cmdArg.type === CommandArgument_1.CommandArgumentType.coordinate) {
                    _coordinatePos++;
                    if (_coordinatePos >= 3) {
                        _coordinatePos = 0;
                    }
                    if (_coordinatePos === 0) {
                        cmdArg.location = CommandArgument_1.CoordinateLocation.x;
                    }
                    else if (_coordinatePos === 1) {
                        cmdArg.location = CommandArgument_1.CoordinateLocation.y;
                    }
                    else if (_coordinatePos === 2) {
                        cmdArg.location = CommandArgument_1.CoordinateLocation.z;
                    }
                }
                else {
                    _coordinatePos = -1;
                }
                this._arguments.push(cmdArg);
            }
        }
        if (this._commandName.startsWith("/")) {
            this._commandName = this._commandName.substring(1, this._commandName.length);
        }
    }
    absolutizeCoordinates(absoluteX, absoluteY, absoluteZ) {
        for (let i = 0; i < this._arguments.length; i++) {
            const arg = this._arguments[i];
            if (arg.type === CommandArgument_1.CommandArgumentType.coordinate) {
                if (arg.coordinateType === CommandArgument_1.CoordinateType.relativeTilde) {
                    if (arg.location === CommandArgument_1.CoordinateLocation.x) {
                        arg.coordinateType = CommandArgument_1.CoordinateType.absolute;
                        arg.position = absoluteX + arg.position;
                    }
                    else if (arg.location === CommandArgument_1.CoordinateLocation.y) {
                        arg.coordinateType = CommandArgument_1.CoordinateType.absolute;
                        arg.position = absoluteY + arg.position;
                    }
                    else if (arg.location === CommandArgument_1.CoordinateLocation.z) {
                        arg.coordinateType = CommandArgument_1.CoordinateType.absolute;
                        arg.position = absoluteZ + arg.position;
                    }
                }
            }
        }
    }
    get hasRelativeOrLocalCoordinates() {
        for (let i = 0; i < this._arguments.length; i++) {
            const arg = this._arguments[i];
            if (arg.type === CommandArgument_1.CommandArgumentType.coordinate && arg.coordinateType !== CommandArgument_1.CoordinateType.absolute) {
                return true;
            }
        }
        return false;
    }
    get firstX() {
        for (let i = 0; i < this._arguments.length; i++) {
            const arg = this._arguments[i];
            if (arg.type === CommandArgument_1.CommandArgumentType.coordinate && arg.location === CommandArgument_1.CoordinateLocation.x) {
                return arg;
            }
        }
        return undefined;
    }
    get firstY() {
        for (let i = 0; i < this._arguments.length; i++) {
            const arg = this._arguments[i];
            if (arg.type === CommandArgument_1.CommandArgumentType.coordinate && arg.location === CommandArgument_1.CoordinateLocation.y) {
                return arg;
            }
        }
        return undefined;
    }
    get firstZ() {
        for (let i = 0; i < this._arguments.length; i++) {
            const arg = this._arguments[i];
            if (arg.type === CommandArgument_1.CommandArgumentType.coordinate && arg.location === CommandArgument_1.CoordinateLocation.z) {
                return arg;
            }
        }
        return undefined;
    }
    toString() {
        let commandResult = this._commandName;
        for (let i = 0; i < this._arguments.length; i++) {
            commandResult += " ";
            commandResult += this._arguments[i].toString();
        }
        return commandResult;
    }
}
exports.default = Command;

//# sourceMappingURL=../maps/minecraft/Command.js.map
