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 { CombinedVisitorAdapter } from '../../combined/visitors/CombinedVisitorAdapter';
import { LoggerCompat } from '../../compat/LoggerCompat';
import { NodeCompat } from '../../compat/NodeCompat';
import { VisitorUtil } from '../../core/util/VisitorUtil';
import { TraverserDirection } from '../../core/visitors/TraverserDirection';
/**
 * A command used to rename a response definition, along with all references to it.
 * @author eric.wittmann@gmail.com
 * @extends AbstractCommand
 * @class
 */
var RenameResponseDefinitionCommand = /** @class */ (function (_super) {
    __extends(RenameResponseDefinitionCommand, _super);
    function RenameResponseDefinitionCommand(oldName, newName) {
        var _this = this;
        if (((typeof oldName === 'string') || oldName === null) && ((typeof newName === 'string') || newName === null)) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._oldName === undefined)
                _this._oldName = null;
            if (_this._newName === undefined)
                _this._newName = null;
            if (_this._references === undefined)
                _this._references = null;
            if (_this._oldName === undefined)
                _this._oldName = null;
            if (_this._newName === undefined)
                _this._newName = null;
            if (_this._references === undefined)
                _this._references = null;
            (function () {
                _this._oldName = oldName;
                _this._newName = newName;
            })();
        }
        else if (oldName === undefined && newName === undefined) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._oldName === undefined)
                _this._oldName = null;
            if (_this._newName === undefined)
                _this._newName = null;
            if (_this._references === undefined)
                _this._references = null;
            if (_this._oldName === undefined)
                _this._oldName = null;
            if (_this._newName === undefined)
                _this._newName = null;
            if (_this._references === undefined)
                _this._references = 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
     */
    RenameResponseDefinitionCommand.prototype.execute = function (document) {
        LoggerCompat.info("[RenameResponseDefinitionCommand] Executing.");
        this._references = ([]);
        if (this._renameResponseDefinition(document, this._oldName, this._newName)) {
            var oldRef = this._nameToReference(this._oldName);
            var newRef = this._nameToReference(this._newName);
            var responseFinder = new RenameResponseDefinitionCommand.ResponseRefFinder(oldRef);
            var responses = responseFinder.findIn(document);
            for (var index195 = 0; index195 < responses.length; index195++) {
                var response = responses[index195];
                {
                    /* add */ (this._references.push(Library.createNodePath(response)) > 0);
                    response.$ref = newRef;
                }
            }
        }
    };
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    RenameResponseDefinitionCommand.prototype.undo = function (document) {
        LoggerCompat.info("[RenameResponseDefinitionCommand] Reverting.");
        if (this._renameResponseDefinition(document, this._newName, this._oldName)) {
            var oldRef = this._nameToReference(this._oldName);
            if (ModelUtils.isDefined(this._references)) {
                this._references.forEach((function (oldRef) {
                    return function (ref) {
                        var response = ref.resolve(document);
                        response.$ref = oldRef;
                    };
                })(oldRef));
            }
        }
    };
    return RenameResponseDefinitionCommand;
}(AbstractCommand));
export { RenameResponseDefinitionCommand };
RenameResponseDefinitionCommand["__class"] = "io.apicurio.datamodels.cmd.commands.RenameResponseDefinitionCommand";
RenameResponseDefinitionCommand["__interfaces"] = ["io.apicurio.datamodels.cmd.ICommand"];
(function (RenameResponseDefinitionCommand) {
    /**
     * Constructor.
     * @param {string} reference
     * @class
     * @extends CombinedVisitorAdapter
     */
    var ResponseRefFinder = /** @class */ (function (_super) {
        __extends(ResponseRefFinder, _super);
        function ResponseRefFinder(reference) {
            var _this = _super.call(this) || this;
            if (_this._reference === undefined)
                _this._reference = null;
            _this._responses = ([]);
            _this._reference = reference;
            return _this;
        }
        ResponseRefFinder.prototype.findIn = function (document) {
            VisitorUtil.visitTree(document, this, TraverserDirection.down);
            return this._responses;
        };
        ResponseRefFinder.prototype._accept = function (response) {
            return ModelUtils.isDefined(response.$ref) && NodeCompat.equals(response.$ref, this._reference);
        };
        /**
         * @see io.apicurio.datamodels.combined.visitors.CombinedVisitorAdapter#visitResponse(io.apicurio.datamodels.openapi.models.OasResponse)
         * @param {OasResponse} node
         */
        ResponseRefFinder.prototype.visitResponse = function (node) {
            if (this._accept(node)) {
                /* add */ (this._responses.push(node) > 0);
            }
        };
        return ResponseRefFinder;
    }(CombinedVisitorAdapter));
    RenameResponseDefinitionCommand.ResponseRefFinder = ResponseRefFinder;
    ResponseRefFinder["__class"] = "io.apicurio.datamodels.cmd.commands.RenameResponseDefinitionCommand.ResponseRefFinder";
    ResponseRefFinder["__interfaces"] = ["io.apicurio.datamodels.core.visitors.IVisitor", "io.apicurio.datamodels.asyncapi.v2.visitors.IAai20Visitor", "io.apicurio.datamodels.openapi.v3.visitors.IOas30Visitor", "io.apicurio.datamodels.asyncapi.visitors.IAaiVisitor", "io.apicurio.datamodels.openapi.v2.visitors.IOas20Visitor", "io.apicurio.datamodels.openapi.visitors.IOasVisitor"];
})(RenameResponseDefinitionCommand || (RenameResponseDefinitionCommand = {}));
//# sourceMappingURL=RenameResponseDefinitionCommand.js.map