var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
/* Generated from Java with JSweet 2.3.7 - http://www.jsweet.org */
import { Library } from '../../Library';
import { AbstractCommand } from '../AbstractCommand';
import { LoggerCompat } from '../../compat/LoggerCompat';
import { NodeCompat } from '../../compat/NodeCompat';
import { Constants } from '../../core/Constants';
import { Server } from '../../core/models/common/Server';
/**
 * A command used to delete a single server from an operation.
 * @author eric.wittmann@gmail.com
 * @extends AbstractCommand
 * @class
 */
var DeleteServerCommand = /** @class */ (function (_super) {
    __extends(DeleteServerCommand, _super);
    function DeleteServerCommand(server) {
        var _this = this;
        if (((server != null && server instanceof Server) || server === null)) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._serverUrl === undefined)
                _this._serverUrl = null;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._oldServer === undefined)
                _this._oldServer = null;
            if (_this._serverUrl === undefined)
                _this._serverUrl = null;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._oldServer === undefined)
                _this._oldServer = null;
            (function () {
                _this._serverUrl = server.url;
                _this._parentPath = Library.createNodePath(server.parent());
            })();
        }
        else if (server === undefined) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._serverUrl === undefined)
                _this._serverUrl = null;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._oldServer === undefined)
                _this._oldServer = null;
            if (_this._serverUrl === undefined)
                _this._serverUrl = null;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._oldServer === undefined)
                _this._oldServer = null;
        }
        else
            throw new Error('invalid overload');
        return _this;
    }
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#execute(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    DeleteServerCommand.prototype.execute = function (document) {
        LoggerCompat.info("[DeleteServerCommand] Executing.");
        this._oldServer = null;
        var parent = this._parentPath.resolve(document);
        if (this.isNullOrUndefined(parent)) {
            return;
        }
        var server = parent.getServer(this._serverUrl);
        if (this.isNullOrUndefined(server)) {
            return;
        }
        var servers = parent.getServers();
        /* remove */ servers.splice(servers.indexOf(server), 1)[0];
        if ( /* size */servers.length === 0) {
            NodeCompat.setProperty(parent, Constants.PROP_SERVERS, null);
        }
        this._oldServer = Library.writeNode(server);
    };
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    DeleteServerCommand.prototype.undo = function (document) {
        LoggerCompat.info("[DeleteServerCommand] Reverting.");
        if (this.isNullOrUndefined(this._oldServer)) {
            return;
        }
        var parent = this._parentPath.resolve(document);
        if (this.isNullOrUndefined(parent)) {
            return;
        }
        var server = parent.createServer();
        Library.readNode(this._oldServer, server);
        var servers = parent.getServers();
        if (this.isNullOrUndefined(servers)) {
            servers = ([]);
            NodeCompat.setProperty(parent, Constants.PROP_SERVERS, servers);
        }
        /* add */ (servers.push(server) > 0);
    };
    return DeleteServerCommand;
}(AbstractCommand));
export { DeleteServerCommand };
DeleteServerCommand["__class"] = "io.apicurio.datamodels.cmd.commands.DeleteServerCommand";
DeleteServerCommand["__interfaces"] = ["io.apicurio.datamodels.cmd.ICommand"];
//# sourceMappingURL=DeleteServerCommand.js.map