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';
/**
 * A command used to rename a parameter.
 * @author eric.wittmann@gmail.com
 * @extends AbstractCommand
 * @class
 */
var RenameParameterCommand = /** @class */ (function (_super) {
    __extends(RenameParameterCommand, _super);
    function RenameParameterCommand(parent, oldParamName, newParamName, paramIn) {
        var _this = this;
        if (((parent != null && (parent["__interfaces"] != null && parent["__interfaces"].indexOf("io.apicurio.datamodels.openapi.models.IOasParameterParent") >= 0 || parent.constructor != null && parent.constructor["__interfaces"] != null && parent.constructor["__interfaces"].indexOf("io.apicurio.datamodels.openapi.models.IOasParameterParent") >= 0)) || parent === null) && ((typeof oldParamName === 'string') || oldParamName === null) && ((typeof newParamName === 'string') || newParamName === null) && ((typeof paramIn === 'string') || paramIn === null)) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._oldParamName === undefined)
                _this._oldParamName = null;
            if (_this._newParamName === undefined)
                _this._newParamName = null;
            if (_this._paramIn === undefined)
                _this._paramIn = null;
            if (_this.isPathItem === undefined)
                _this.isPathItem = false;
            if (_this.isOperation === undefined)
                _this.isOperation = false;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._oldParamName === undefined)
                _this._oldParamName = null;
            if (_this._newParamName === undefined)
                _this._newParamName = null;
            if (_this._paramIn === undefined)
                _this._paramIn = null;
            if (_this.isPathItem === undefined)
                _this.isPathItem = false;
            if (_this.isOperation === undefined)
                _this.isOperation = false;
            (function () {
                _this._oldParamName = oldParamName;
                _this._newParamName = newParamName;
                _this._paramIn = paramIn;
                _this._parentPath = Library.createNodePath(parent);
            })();
        }
        else if (parent === undefined && oldParamName === undefined && newParamName === undefined && paramIn === undefined) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._oldParamName === undefined)
                _this._oldParamName = null;
            if (_this._newParamName === undefined)
                _this._newParamName = null;
            if (_this._paramIn === undefined)
                _this._paramIn = null;
            if (_this.isPathItem === undefined)
                _this.isPathItem = false;
            if (_this.isOperation === undefined)
                _this.isOperation = false;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._oldParamName === undefined)
                _this._oldParamName = null;
            if (_this._newParamName === undefined)
                _this._newParamName = null;
            if (_this._paramIn === undefined)
                _this._paramIn = null;
            if (_this.isPathItem === undefined)
                _this.isPathItem = false;
            if (_this.isOperation === undefined)
                _this.isOperation = 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
     */
    RenameParameterCommand.prototype.execute = function (document) {
        LoggerCompat.info("[RenameParameterCommand] Executing.");
        this._doParameterRename(document, this._oldParamName, this._newParamName);
    };
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    RenameParameterCommand.prototype.undo = function (document) {
        LoggerCompat.info("[RenameParameterCommand] Reverting.");
        this._doParameterRename(document, this._newParamName, this._oldParamName);
    };
    /**
     * Does the work of renaming a param from one name to another.
     * @param {Document} document
     * @param {string} from
     * @param {string} to
     * @private
     */
    /*private*/ RenameParameterCommand.prototype._doParameterRename = function (document, from, to) {
        var parent = this._parentPath.resolve(document);
        if (this.isNullOrUndefined(parent)) {
            return;
        }
        var param = parent.getParameter(this._paramIn, from);
        if (this.isNullOrUndefined(param)) {
            return;
        }
        var allParams = ([]);
        /* add */ (allParams.push(param) > 0);
        this.isPathItem = false;
        this.isOperation = false;
        VisitorUtil.visitNode(parent, new RenameParameterCommand.RenameParameterCommand$0(this));
        var methods = NodeCompat.asList("get", "put", "post", "delete", "options", "head", "patch", "trace");
        if (this.isPathItem) {
            var pathItem = parent;
            for (var index208 = 0; index208 < methods.length; index208++) {
                var method = methods[index208];
                {
                    var op = NodeCompat.getProperty(pathItem, method);
                    if (!this.isNullOrUndefined(op)) {
                        var opParam = op.getParameter(this._paramIn, from);
                        if (!this.isNullOrUndefined(opParam)) {
                            /* add */ (allParams.push(opParam) > 0);
                        }
                    }
                }
            }
        }
        if (this.isOperation) {
            var operation = parent;
            var pathItem = operation.parent();
            var pparam = pathItem.getParameter(this._paramIn, from);
            if (!this.isNullOrUndefined(pparam)) {
                /* add */ (allParams.push(pparam) > 0);
                for (var index209 = 0; index209 < methods.length; index209++) {
                    var method = methods[index209];
                    {
                        var peerOperation = NodeCompat.getProperty(pathItem, method);
                        if (ModelUtils.isDefined(peerOperation) && peerOperation !== operation) {
                            var opParam = peerOperation.getParameter(this._paramIn, from);
                            if (!this.isNullOrUndefined(opParam)) {
                                /* add */ (allParams.push(opParam) > 0);
                            }
                        }
                    }
                }
            }
        }
        allParams.forEach(function (p) {
            p.name = to;
        });
    };
    return RenameParameterCommand;
}(AbstractCommand));
export { RenameParameterCommand };
RenameParameterCommand["__class"] = "io.apicurio.datamodels.cmd.commands.RenameParameterCommand";
RenameParameterCommand["__interfaces"] = ["io.apicurio.datamodels.cmd.ICommand"];
(function (RenameParameterCommand) {
    var RenameParameterCommand$0 = /** @class */ (function (_super) {
        __extends(RenameParameterCommand$0, _super);
        function RenameParameterCommand$0(__parent) {
            var _this = _super.call(this) || this;
            _this.__parent = __parent;
            return _this;
        }
        /**
         *
         * @param {OasPathItem} node
         */
        RenameParameterCommand$0.prototype.visitPathItem = function (node) {
            this.__parent.isPathItem = true;
        };
        /**
         *
         * @param {Operation} node
         */
        RenameParameterCommand$0.prototype.visitOperation = function (node) {
            this.__parent.isOperation = true;
        };
        return RenameParameterCommand$0;
    }(CombinedVisitorAdapter));
    RenameParameterCommand.RenameParameterCommand$0 = RenameParameterCommand$0;
    RenameParameterCommand$0["__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"];
})(RenameParameterCommand || (RenameParameterCommand = {}));
//# sourceMappingURL=RenameParameterCommand.js.map