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 { LoggerCompat } from '../../compat/LoggerCompat';
import { OasSchema } from '../../openapi/models/OasSchema';
/**
 * A command used to rename a schema property.
 * @author eric.wittmann@gmail.com
 * @extends AbstractCommand
 * @class
 */
var RenamePropertyCommand = /** @class */ (function (_super) {
    __extends(RenamePropertyCommand, _super);
    function RenamePropertyCommand(parent, oldPropertyName, newPropertyName) {
        var _this = this;
        if (((parent != null && parent instanceof OasSchema) || parent === null) && ((typeof oldPropertyName === 'string') || oldPropertyName === null) && ((typeof newPropertyName === 'string') || newPropertyName === null)) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._oldPropertyName === undefined)
                _this._oldPropertyName = null;
            if (_this._newPropertyName === undefined)
                _this._newPropertyName = null;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._oldPropertyName === undefined)
                _this._oldPropertyName = null;
            if (_this._newPropertyName === undefined)
                _this._newPropertyName = null;
            (function () {
                _this._oldPropertyName = oldPropertyName;
                _this._newPropertyName = newPropertyName;
                _this._parentPath = Library.createNodePath(parent);
            })();
        }
        else if (parent === undefined && oldPropertyName === undefined && newPropertyName === undefined) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._oldPropertyName === undefined)
                _this._oldPropertyName = null;
            if (_this._newPropertyName === undefined)
                _this._newPropertyName = null;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._oldPropertyName === undefined)
                _this._oldPropertyName = null;
            if (_this._newPropertyName === undefined)
                _this._newPropertyName = 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
     */
    RenamePropertyCommand.prototype.execute = function (document) {
        LoggerCompat.info("[RenamePropertyCommand] Executing.");
        this._doPropertyRename(document, this._oldPropertyName, this._newPropertyName);
    };
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    RenamePropertyCommand.prototype.undo = function (document) {
        LoggerCompat.info("[RenamePropertyCommand] Reverting.");
        this._doPropertyRename(document, this._newPropertyName, this._oldPropertyName);
    };
    /**
     * Does the work of renaming a path from one name to another.
     * @param {Document} document
     * @param {string} from
     * @param {string} to
     * @private
     * @private
     */
    /*private*/ RenamePropertyCommand.prototype._doPropertyRename = function (document, from, to) {
        var parent = this._parentPath.resolve(document);
        if (this.isNullOrUndefined(parent)) {
            return;
        }
        if (!this.isNullOrUndefined(parent.getProperty(to))) {
            return;
        }
        var property = parent.removeProperty(from);
        if (this.isNullOrUndefined(property)) {
            return;
        }
        property.rename(to);
        parent.addProperty(to, property);
        var required = parent.required;
        var reqIdx = ModelUtils.isDefined(required) ? required.indexOf(from) : -1;
        if (reqIdx !== -1) {
            /* set */ (parent.required[reqIdx] = to);
        }
    };
    return RenamePropertyCommand;
}(AbstractCommand));
export { RenamePropertyCommand };
RenamePropertyCommand["__class"] = "io.apicurio.datamodels.cmd.commands.RenamePropertyCommand";
RenamePropertyCommand["__interfaces"] = ["io.apicurio.datamodels.cmd.ICommand"];
//# sourceMappingURL=RenamePropertyCommand.js.map