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';
/**
 * Base class for commands that replace entire nodes.
 * @author eric.wittmann@gmail.com
 * @extends AbstractCommand
 * @class
 */
var ReplaceNodeCommand = /** @class */ (function (_super) {
    __extends(ReplaceNodeCommand, _super);
    function ReplaceNodeCommand(old, replacement) {
        var _this = this;
        if (((old != null) || old === null) && ((replacement != null) || replacement === null)) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._nodePath === undefined)
                _this._nodePath = null;
            if (_this._new === undefined)
                _this._new = null;
            if (_this._old === undefined)
                _this._old = null;
            if (_this._nodePath === undefined)
                _this._nodePath = null;
            if (_this._new === undefined)
                _this._new = null;
            if (_this._old === undefined)
                _this._old = null;
            (function () {
                _this._nodePath = Library.createNodePath(old);
                _this._new = Library.writeNode(replacement);
            })();
        }
        else if (old === undefined && replacement === undefined) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._nodePath === undefined)
                _this._nodePath = null;
            if (_this._new === undefined)
                _this._new = null;
            if (_this._old === undefined)
                _this._old = null;
            if (_this._nodePath === undefined)
                _this._nodePath = null;
            if (_this._new === undefined)
                _this._new = null;
            if (_this._old === undefined)
                _this._old = 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
     */
    ReplaceNodeCommand.prototype.execute = function (document) {
        LoggerCompat.info("[AbstractReplaceNodeCommand] Executing.");
        this._old = null;
        var oldNode = this._nodePath.resolve(document);
        if (this.isNullOrUndefined(oldNode)) {
            return;
        }
        this._old = Library.writeNode(oldNode);
        this.removeNode(document, oldNode);
        var newNode = this.readNode(document, this._new);
        this.addNode(document, newNode);
    };
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    ReplaceNodeCommand.prototype.undo = function (document) {
        LoggerCompat.info("[AbstractReplaceNodeCommand] Reverting.");
        if (this.isNullOrUndefined(this._old)) {
            return;
        }
        var node = this._nodePath.resolve(document);
        if (this.isNullOrUndefined(node)) {
            return;
        }
        this.removeNode(document, node);
        var restoreNode = this.readNode(document, this._old);
        this.addNode(document, restoreNode);
    };
    return ReplaceNodeCommand;
}(AbstractCommand));
export { ReplaceNodeCommand };
ReplaceNodeCommand["__class"] = "io.apicurio.datamodels.cmd.commands.ReplaceNodeCommand";
ReplaceNodeCommand["__interfaces"] = ["io.apicurio.datamodels.cmd.ICommand"];
//# sourceMappingURL=ReplaceNodeCommand.js.map