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 { NodeCompat } from '../../compat/NodeCompat';
import { VisitorUtil } from '../../core/util/VisitorUtil';
import { TraverserDirection } from '../../core/visitors/TraverserDirection';
/**
 * A command used to rename a tag, along with all references to it.
 * @author eric.wittmann@gmail.com
 * @extends AbstractCommand
 * @class
 */
var RenameTagDefinitionCommand = /** @class */ (function (_super) {
    __extends(RenameTagDefinitionCommand, _super);
    function RenameTagDefinitionCommand(oldTag, newTag) {
        var _this = this;
        if (((typeof oldTag === 'string') || oldTag === null) && ((typeof newTag === 'string') || newTag === null)) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._oldTag === undefined)
                _this._oldTag = null;
            if (_this._newTag === undefined)
                _this._newTag = null;
            if (_this._oldTag === undefined)
                _this._oldTag = null;
            if (_this._newTag === undefined)
                _this._newTag = null;
            (function () {
                _this._oldTag = oldTag;
                _this._newTag = newTag;
            })();
        }
        else if (oldTag === undefined && newTag === undefined) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._oldTag === undefined)
                _this._oldTag = null;
            if (_this._newTag === undefined)
                _this._newTag = null;
            if (_this._oldTag === undefined)
                _this._oldTag = null;
            if (_this._newTag === undefined)
                _this._newTag = 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
     */
    RenameTagDefinitionCommand.prototype.execute = function (document) {
        LoggerCompat.info("[RenameTagDefinitionCommand] Executing.");
        this._doTagRename(document, this._oldTag, this._newTag);
    };
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    RenameTagDefinitionCommand.prototype.undo = function (document) {
        LoggerCompat.info("[RenameTagDefinitionCommand] Reverting.");
        this._doTagRename(document, this._newTag, this._oldTag);
    };
    /**
     * Does the work of renaming a tag from one name to another.
     * @param {Document} document
     * @param {string} from
     * @param {string} to
     * @private
     * @private
     */
    RenameTagDefinitionCommand.prototype._doTagRename = function (document, from, to) {
        var existingTag = this._findTag(document, to);
        if (ModelUtils.isDefined(existingTag)) {
            return;
        }
        var tagToRename = this._findTag(document, from);
        if (ModelUtils.isDefined(tagToRename)) {
            tagToRename.name = to;
            var tagRenamer = new RenameTagDefinitionCommand.TagRenameVisitor(from, to);
            VisitorUtil.visitTree(document, tagRenamer, TraverserDirection.down);
        }
    };
    /**
     * Finds a tag in the document by name.  Returns null if not found.
     * @param {Document} document
     * @param {string} tag
     * @private
     * @return {Tag}
     * @private
     */
    RenameTagDefinitionCommand.prototype._findTag = function (document, tag) {
        if (ModelUtils.isDefined(document.tags)) {
            for (var index206 = 0; index206 < document.tags.length; index206++) {
                var tagDef = document.tags[index206];
                {
                    if (NodeCompat.equals(tagDef.name, tag)) {
                        return tagDef;
                    }
                }
            }
        }
        return null;
    };
    return RenameTagDefinitionCommand;
}(AbstractCommand));
export { RenameTagDefinitionCommand };
RenameTagDefinitionCommand["__class"] = "io.apicurio.datamodels.cmd.commands.RenameTagDefinitionCommand";
RenameTagDefinitionCommand["__interfaces"] = ["io.apicurio.datamodels.cmd.ICommand"];
(function (RenameTagDefinitionCommand) {
    /**
     * Constructor.
     * @param {string} from
     * @param {string} to
     * @class
     * @extends CombinedVisitorAdapter
     */
    var TagRenameVisitor = /** @class */ (function (_super) {
        __extends(TagRenameVisitor, _super);
        function TagRenameVisitor(from, to) {
            var _this = _super.call(this) || this;
            if (_this.from === undefined)
                _this.from = null;
            if (_this.to === undefined)
                _this.to = null;
            _this.from = from;
            _this.to = to;
            return _this;
        }
        /**
         * @see io.apicurio.datamodels.combined.visitors.CombinedVisitorAdapter#visitOperation(io.apicurio.datamodels.core.models.common.Operation)
         * @param {Operation} node
         */
        TagRenameVisitor.prototype.visitOperation = function (node) {
            var op = node;
            var idx = ModelUtils.isDefined(op.tags) ? op.tags.indexOf(this.from) : -1;
            if (idx !== -1) {
                /* set */ (op.tags[idx] = this.to);
            }
        };
        return TagRenameVisitor;
    }(CombinedVisitorAdapter));
    RenameTagDefinitionCommand.TagRenameVisitor = TagRenameVisitor;
    TagRenameVisitor["__class"] = "io.apicurio.datamodels.cmd.commands.RenameTagDefinitionCommand.TagRenameVisitor";
    TagRenameVisitor["__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"];
})(RenameTagDefinitionCommand || (RenameTagDefinitionCommand = {}));
//# sourceMappingURL=RenameTagDefinitionCommand.js.map