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 { AbstractCommand } from '../AbstractCommand';
import { ModelUtils } from '../util/ModelUtils';
import { CombinedVisitorAdapter } from '../../combined/visitors/CombinedVisitorAdapter';
import { LoggerCompat } from '../../compat/LoggerCompat';
import { VisitorUtil } from '../../core/util/VisitorUtil';
import { TraverserDirection } from '../../core/visitors/TraverserDirection';
/**
 * A command used to rename a security scheme, along with all references to it.
 * @author eric.wittmann@gmail.com
 * @extends AbstractCommand
 * @class
 */
var RenameSecuritySchemeCommand = /** @class */ (function (_super) {
    __extends(RenameSecuritySchemeCommand, _super);
    function RenameSecuritySchemeCommand(oldSchemeName, newSchemeName) {
        var _this = this;
        if (((typeof oldSchemeName === 'string') || oldSchemeName === null) && ((typeof newSchemeName === 'string') || newSchemeName === null)) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._oldSchemeName === undefined)
                _this._oldSchemeName = null;
            if (_this._newSchemeName === undefined)
                _this._newSchemeName = null;
            if (_this._oldSchemeName === undefined)
                _this._oldSchemeName = null;
            if (_this._newSchemeName === undefined)
                _this._newSchemeName = null;
            (function () {
                _this._oldSchemeName = oldSchemeName;
                _this._newSchemeName = newSchemeName;
            })();
        }
        else if (oldSchemeName === undefined && newSchemeName === undefined) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._oldSchemeName === undefined)
                _this._oldSchemeName = null;
            if (_this._newSchemeName === undefined)
                _this._newSchemeName = null;
            if (_this._oldSchemeName === undefined)
                _this._oldSchemeName = null;
            if (_this._newSchemeName === undefined)
                _this._newSchemeName = 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
     */
    RenameSecuritySchemeCommand.prototype.execute = function (document) {
        LoggerCompat.info("[RenameSecuritySchemeCommand] Executing.");
        this._doSecuritySchemeRename(document, this._oldSchemeName, this._newSchemeName);
    };
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    RenameSecuritySchemeCommand.prototype.undo = function (document) {
        LoggerCompat.info("[RenameSecuritySchemeCommand] Reverting.");
        this._doSecuritySchemeRename(document, this._newSchemeName, this._oldSchemeName);
    };
    /**
     * Does the work of renaming a path from one name to another.
     * @param {OasDocument} document
     * @param {string} from
     * @param {string} to
     * @private
     * @private
     */
    /*private*/ RenameSecuritySchemeCommand.prototype._doSecuritySchemeRename = function (document, from, to) {
        var scheme = null;
        if (document.is2xDocument()) {
            var doc20 = document;
            if (ModelUtils.isDefined(doc20.securityDefinitions)) {
                if (ModelUtils.isDefined(doc20.securityDefinitions.getSecurityScheme(to))) {
                    return;
                }
                scheme = doc20.securityDefinitions.removeSecurityScheme(from);
            }
        }
        else {
            var doc30 = document;
            if (ModelUtils.isDefined(doc30.components)) {
                if (!this.isNullOrUndefined(doc30.components.getSecurityScheme(to))) {
                    return;
                }
                scheme = doc30.components.removeSecurityScheme(from);
            }
        }
        if (this.isNullOrUndefined(scheme)) {
            return;
        }
        scheme.rename(to);
        if (document.is2xDocument()) {
            var doc20 = document;
            doc20.securityDefinitions.addSecurityScheme(to, scheme);
        }
        else {
            var doc30 = document;
            doc30.components.addSecurityScheme(to, scheme);
        }
        VisitorUtil.visitTree(document, new RenameSecuritySchemeCommand.RenameSecuritySchemeCommand$0(this, from, to), TraverserDirection.down);
    };
    return RenameSecuritySchemeCommand;
}(AbstractCommand));
export { RenameSecuritySchemeCommand };
RenameSecuritySchemeCommand["__class"] = "io.apicurio.datamodels.cmd.commands.RenameSecuritySchemeCommand";
RenameSecuritySchemeCommand["__interfaces"] = ["io.apicurio.datamodels.cmd.ICommand"];
(function (RenameSecuritySchemeCommand) {
    var RenameSecuritySchemeCommand$0 = /** @class */ (function (_super) {
        __extends(RenameSecuritySchemeCommand$0, _super);
        function RenameSecuritySchemeCommand$0(__parent, from, to) {
            var _this = _super.call(this) || this;
            _this.from = from;
            _this.to = to;
            _this.__parent = __parent;
            return _this;
        }
        /**
         *
         * @param {SecurityRequirement} node
         */
        RenameSecuritySchemeCommand$0.prototype.visitSecurityRequirement = function (node) {
            var scopes = node.removeSecurityRequirementItem(this.from);
            if (ModelUtils.isDefined(scopes)) {
                node.addSecurityRequirementItem(this.to, scopes);
            }
        };
        return RenameSecuritySchemeCommand$0;
    }(CombinedVisitorAdapter));
    RenameSecuritySchemeCommand.RenameSecuritySchemeCommand$0 = RenameSecuritySchemeCommand$0;
    RenameSecuritySchemeCommand$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"];
})(RenameSecuritySchemeCommand || (RenameSecuritySchemeCommand = {}));
//# sourceMappingURL=RenameSecuritySchemeCommand.js.map