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 { ModelUtils } from '../util/ModelUtils';
import { JsonCompat } from '../../compat/JsonCompat';
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 create a new server in a document.
 * @author eric.wittmann@gmail.com
 * @extends AbstractCommand
 * @class
 */
var NewServerCommand = /** @class */ (function (_super) {
    __extends(NewServerCommand, _super);
    function NewServerCommand(parent, server) {
        var _this = this;
        if (((parent != null && (parent["__interfaces"] != null && parent["__interfaces"].indexOf("io.apicurio.datamodels.core.models.common.IServerParent") >= 0 || parent.constructor != null && parent.constructor["__interfaces"] != null && parent.constructor["__interfaces"].indexOf("io.apicurio.datamodels.core.models.common.IServerParent") >= 0)) || parent === null) && ((server != null && server instanceof Server) || server === null)) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._server === undefined)
                _this._server = null;
            if (_this._serverExisted === undefined)
                _this._serverExisted = false;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._server === undefined)
                _this._server = null;
            if (_this._serverExisted === undefined)
                _this._serverExisted = false;
            (function () {
                _this._parentPath = Library.createNodePath(parent);
                _this._server = Library.writeNode(server);
            })();
        }
        else if (parent === undefined && server === undefined) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._server === undefined)
                _this._server = null;
            if (_this._serverExisted === undefined)
                _this._serverExisted = false;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._server === undefined)
                _this._server = null;
            if (_this._serverExisted === undefined)
                _this._serverExisted = false;
        }
        else
            throw new Error('invalid overload');
        return _this;
    }
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#execute(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    NewServerCommand.prototype.execute = function (document) {
        LoggerCompat.info("[NewServerCommand] Executing.");
        var parent = document;
        if (!this.isNullOrUndefined(this._parentPath)) {
            parent = this._parentPath.resolve(document);
        }
        if (this.isNullOrUndefined(parent)) {
            return;
        }
        var servers = parent.getServers();
        if (this.isNullOrUndefined(servers)) {
            servers = ([]);
            NodeCompat.setProperty(parent, Constants.PROP_SERVERS, servers);
        }
        var server = parent.createServer();
        Library.readNode(this._server, server);
        if (this.serverAlreadyExists(parent, server)) {
            this._serverExisted = true;
            return;
        }
        else {
            /* add */ (servers.push(server) > 0);
            this._serverExisted = false;
        }
    };
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    NewServerCommand.prototype.undo = function (document) {
        LoggerCompat.info("[NewServerCommand] Reverting.");
        if (this._serverExisted) {
            return;
        }
        var parent = document;
        if (ModelUtils.isDefined(this._parentPath)) {
            parent = this._parentPath.resolve(document);
        }
        if (this.isNullOrUndefined(parent) || this.isNullOrUndefined(parent.getServers())) {
            return;
        }
        var servers = parent.getServers();
        var serverUrl = JsonCompat.getPropertyString(this._server, Constants.PROP_URL);
        var server = parent.getServer(serverUrl);
        if (this.isNullOrUndefined(server)) {
            return;
        }
        /* remove */ servers.splice(servers.indexOf(server), 1)[0];
        if ( /* size */servers.length === 0) {
            NodeCompat.setProperty(parent, Constants.PROP_SERVERS, null);
        }
    };
    /**
     * Returns true if a server with the same url already exists in the parent.
     * @param {*} parent
     * @param {Server} server
     * @return {boolean}
     * @private
     */
    /*private*/ NewServerCommand.prototype.serverAlreadyExists = function (parent, server) {
        return ModelUtils.isDefined(parent.getServer(server.url));
    };
    return NewServerCommand;
}(AbstractCommand));
export { NewServerCommand };
NewServerCommand["__class"] = "io.apicurio.datamodels.cmd.commands.NewServerCommand";
NewServerCommand["__interfaces"] = ["io.apicurio.datamodels.cmd.ICommand"];
//# sourceMappingURL=NewServerCommand.js.map