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 schema definition, along with all references to it.
 * @author eric.wittmann@gmail.com
 * @extends AbstractCommand
 * @class
 */
var RenameSchemaDefinitionCommand = /** @class */ (function (_super) {
    __extends(RenameSchemaDefinitionCommand, _super);
    function RenameSchemaDefinitionCommand(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
     */
    RenameSchemaDefinitionCommand.prototype.execute = function (document) {
        LoggerCompat.info("[RenameSchemaDefinitionCommand] Executing.");
        this._references = ([]);
        if (this._renameSchemaDefinition(document, this._oldName, this._newName)) {
            var oldRef = this._nameToReference(this._oldName);
            var newRef = this._nameToReference(this._newName);
            var schemaFinder = new RenameSchemaDefinitionCommand.SchemaRefFinder(oldRef);
            var schemas = schemaFinder.findIn(document);
            for (var index199 = 0; index199 < schemas.length; index199++) {
                var schema = schemas[index199];
                {
                    /* add */ (this._references.push(Library.createNodePath(schema)) > 0);
                    schema.$ref = newRef;
                }
            }
        }
    };
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    RenameSchemaDefinitionCommand.prototype.undo = function (document) {
        LoggerCompat.info("[RenameSchemaDefinitionCommand] Reverting.");
        if (this._renameSchemaDefinition(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 schema = ref.resolve(document);
                        schema.$ref = oldRef;
                    };
                })(oldRef));
            }
        }
    };
    return RenameSchemaDefinitionCommand;
}(AbstractCommand));
export { RenameSchemaDefinitionCommand };
RenameSchemaDefinitionCommand["__class"] = "io.apicurio.datamodels.cmd.commands.RenameSchemaDefinitionCommand";
RenameSchemaDefinitionCommand["__interfaces"] = ["io.apicurio.datamodels.cmd.ICommand"];
(function (RenameSchemaDefinitionCommand) {
    /**
     * Constructor.
     * @param {string} reference
     * @class
     * @extends CombinedVisitorAdapter
     */
    var SchemaRefFinder = /** @class */ (function (_super) {
        __extends(SchemaRefFinder, _super);
        function SchemaRefFinder(reference) {
            var _this = _super.call(this) || this;
            if (_this._reference === undefined)
                _this._reference = null;
            _this._schemas = ([]);
            _this._reference = reference;
            return _this;
        }
        SchemaRefFinder.prototype.findIn = function (document) {
            VisitorUtil.visitTree(document, this, TraverserDirection.down);
            return this._schemas;
        };
        SchemaRefFinder.prototype._accept = function (schema) {
            return ModelUtils.isDefined(schema.$ref) && NodeCompat.equals(schema.$ref, this._reference);
        };
        SchemaRefFinder.prototype.processSchema = function (schema) {
            if (this._accept(schema)) {
                /* add */ (this._schemas.push(schema) > 0);
            }
        };
        /**
         * @see io.apicurio.datamodels.combined.visitors.CombinedVisitorAdapter#visitSchema(io.apicurio.datamodels.core.models.common.Schema)
         * @param {Schema} node
         */
        SchemaRefFinder.prototype.visitSchema = function (node) {
            this.processSchema(node);
        };
        /**
         * @see io.apicurio.datamodels.combined.visitors.CombinedVisitorAdapter#visitSchemaDefinition(io.apicurio.datamodels.core.models.common.IDefinition)
         * @param {*} node
         */
        SchemaRefFinder.prototype.visitSchemaDefinition = function (node) {
            this.processSchema(node);
        };
        /**
         * @see io.apicurio.datamodels.combined.visitors.CombinedVisitorAdapter#visitPropertySchema(io.apicurio.datamodels.openapi.models.IOasPropertySchema)
         * @param {*} node
         */
        SchemaRefFinder.prototype.visitPropertySchema = function (node) {
            this.processSchema(node);
        };
        /**
         * @see io.apicurio.datamodels.combined.visitors.CombinedVisitorAdapter#visitAdditionalPropertiesSchema(io.apicurio.datamodels.openapi.models.OasSchema)
         * @param {OasSchema} node
         */
        SchemaRefFinder.prototype.visitAdditionalPropertiesSchema = function (node) {
            this.processSchema(node);
        };
        /**
         * @see io.apicurio.datamodels.combined.visitors.CombinedVisitorAdapter#visitAllOfSchema(io.apicurio.datamodels.openapi.models.OasSchema)
         * @param {OasSchema} node
         */
        SchemaRefFinder.prototype.visitAllOfSchema = function (node) {
            this.processSchema(node);
        };
        /**
         * @see io.apicurio.datamodels.combined.visitors.CombinedVisitorAdapter#visitItemsSchema(io.apicurio.datamodels.openapi.models.OasSchema)
         * @param {OasSchema} node
         */
        SchemaRefFinder.prototype.visitItemsSchema = function (node) {
            this.processSchema(node);
        };
        /**
         * @see io.apicurio.datamodels.combined.visitors.CombinedVisitorAdapter#visitAnyOfSchema(io.apicurio.datamodels.openapi.v3.models.Oas30Schema.Oas30AnyOfSchema)
         * @param {Oas30Schema.Oas30AnyOfSchema} node
         */
        SchemaRefFinder.prototype.visitAnyOfSchema = function (node) {
            this.processSchema(node);
        };
        /**
         * @see io.apicurio.datamodels.combined.visitors.CombinedVisitorAdapter#visitOneOfSchema(io.apicurio.datamodels.openapi.v3.models.Oas30Schema.Oas30OneOfSchema)
         * @param {Oas30Schema.Oas30OneOfSchema} node
         */
        SchemaRefFinder.prototype.visitOneOfSchema = function (node) {
            this.processSchema(node);
        };
        /**
         * @see io.apicurio.datamodels.combined.visitors.CombinedVisitorAdapter#visitNotSchema(io.apicurio.datamodels.openapi.v3.models.Oas30Schema.Oas30NotSchema)
         * @param {Oas30Schema.Oas30NotSchema} node
         */
        SchemaRefFinder.prototype.visitNotSchema = function (node) {
            this.processSchema(node);
        };
        return SchemaRefFinder;
    }(CombinedVisitorAdapter));
    RenameSchemaDefinitionCommand.SchemaRefFinder = SchemaRefFinder;
    SchemaRefFinder["__class"] = "io.apicurio.datamodels.cmd.commands.RenameSchemaDefinitionCommand.SchemaRefFinder";
    SchemaRefFinder["__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"];
})(RenameSchemaDefinitionCommand || (RenameSchemaDefinitionCommand = {}));
//# sourceMappingURL=RenameSchemaDefinitionCommand.js.map