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 { Server } from '../../core/models/common/Server';
/**
 * A command used to modify a server.
 * @author eric.wittmann@gmail.com
 * @extends AbstractCommand
 * @class
 */
var ChangeServerCommand = /** @class */ (function (_super) {
    __extends(ChangeServerCommand, _super);
    function ChangeServerCommand(server) {
        var _this = this;
        if (((server != null && server instanceof Server) || server === null)) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._serverUrl === undefined)
                _this._serverUrl = null;
            if (_this._serverObj === undefined)
                _this._serverObj = null;
            if (_this._oldServer === undefined)
                _this._oldServer = null;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._serverUrl === undefined)
                _this._serverUrl = null;
            if (_this._serverObj === undefined)
                _this._serverObj = null;
            if (_this._oldServer === undefined)
                _this._oldServer = null;
            (function () {
                _this._parentPath = Library.createNodePath(server.parent());
                _this._serverUrl = server.url;
                _this._serverObj = Library.writeNode(server);
            })();
        }
        else if (server === undefined) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._serverUrl === undefined)
                _this._serverUrl = null;
            if (_this._serverObj === undefined)
                _this._serverObj = null;
            if (_this._oldServer === undefined)
                _this._oldServer = null;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._serverUrl === undefined)
                _this._serverUrl = null;
            if (_this._serverObj === undefined)
                _this._serverObj = 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
     */
    ChangeServerCommand.prototype.execute = function (document) {
        LoggerCompat.info("[ChangeServerCommand] 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;
        }
        this._oldServer = Library.writeNode(server);
        this.replaceServerWith(server, this._serverObj);
    };
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    ChangeServerCommand.prototype.undo = function (document) {
        LoggerCompat.info("[ChangeServerCommand] Reverting.");
        if (this.isNullOrUndefined(this._oldServer)) {
            return;
        }
        var parent = this._parentPath.resolve(document);
        if (this.isNullOrUndefined(parent)) {
            return;
        }
        var server = parent.getServer(this._serverUrl);
        if (this.isNullOrUndefined(server)) {
            return;
        }
        this.replaceServerWith(server, this._oldServer);
    };
    /**
     * Replaces the content of a server with the content from another server.
     * @param {Server} toServer
     * @param {*} fromServer
     */
    ChangeServerCommand.prototype.replaceServerWith = function (toServer, fromServer) {
        toServer.getServerVariables().forEach(function (var_) {
            toServer.removeServerVariable(var_.getName());
        });
        Library.readNode(fromServer, toServer);
    };
    return ChangeServerCommand;
}(AbstractCommand));
export { ChangeServerCommand };
ChangeServerCommand["__class"] = "io.apicurio.datamodels.cmd.commands.ChangeServerCommand";
ChangeServerCommand["__interfaces"] = ["io.apicurio.datamodels.cmd.ICommand"];
//# sourceMappingURL=ChangeServerCommand.js.map