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';
/**
 * A command used to delete a single property from a schema.
 * @author eric.wittmann@gmail.com
 * @extends AbstractCommand
 * @class
 */
var DeletePropertyCommand = /** @class */ (function (_super) {
    __extends(DeletePropertyCommand, _super);
    function DeletePropertyCommand(property) {
        var _this = this;
        if (((property != null && (property["__interfaces"] != null && property["__interfaces"].indexOf("io.apicurio.datamodels.openapi.models.IOasPropertySchema") >= 0 || property.constructor != null && property.constructor["__interfaces"] != null && property.constructor["__interfaces"].indexOf("io.apicurio.datamodels.openapi.models.IOasPropertySchema") >= 0)) || property === null)) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._propertyName === undefined)
                _this._propertyName = null;
            if (_this._propertyPath === undefined)
                _this._propertyPath = null;
            if (_this._schemaPath === undefined)
                _this._schemaPath = null;
            if (_this._oldProperty === undefined)
                _this._oldProperty = null;
            if (_this._oldRequired === undefined)
                _this._oldRequired = false;
            if (_this._propertyName === undefined)
                _this._propertyName = null;
            if (_this._propertyPath === undefined)
                _this._propertyPath = null;
            if (_this._schemaPath === undefined)
                _this._schemaPath = null;
            if (_this._oldProperty === undefined)
                _this._oldProperty = null;
            if (_this._oldRequired === undefined)
                _this._oldRequired = false;
            (function () {
                _this._propertyName = property.getPropertyName();
                _this._propertyPath = Library.createNodePath(property);
                _this._schemaPath = Library.createNodePath(property.parent());
            })();
        }
        else if (property === undefined) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._propertyName === undefined)
                _this._propertyName = null;
            if (_this._propertyPath === undefined)
                _this._propertyPath = null;
            if (_this._schemaPath === undefined)
                _this._schemaPath = null;
            if (_this._oldProperty === undefined)
                _this._oldProperty = null;
            if (_this._oldRequired === undefined)
                _this._oldRequired = false;
            if (_this._propertyName === undefined)
                _this._propertyName = null;
            if (_this._propertyPath === undefined)
                _this._propertyPath = null;
            if (_this._schemaPath === undefined)
                _this._schemaPath = null;
            if (_this._oldProperty === undefined)
                _this._oldProperty = null;
            if (_this._oldRequired === undefined)
                _this._oldRequired = 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
     */
    DeletePropertyCommand.prototype.execute = function (document) {
        LoggerCompat.info("[DeletePropertyCommand] Executing.");
        this._oldProperty = null;
        var property = this._propertyPath.resolve(document);
        if (this.isNullOrUndefined(property)) {
            return;
        }
        var schema = property.parent();
        this._oldProperty = Library.writeNode(schema.removeProperty(this._propertyName));
        this._oldRequired = ModelUtils.isDefined(schema.required) && schema.required.indexOf(this._propertyName) !== -1;
        if (this._oldRequired) {
            /* remove */ schema.required.splice(schema.required.indexOf(this._propertyName), 1)[0];
        }
    };
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    DeletePropertyCommand.prototype.undo = function (document) {
        LoggerCompat.info("[DeletePropertyCommand] Reverting.");
        if (this.isNullOrUndefined(this._oldProperty)) {
            return;
        }
        var schema = this._schemaPath.resolve(document);
        if (this.isNullOrUndefined(schema)) {
            return;
        }
        var propSchema = schema.createPropertySchema(this._propertyName);
        Library.readNode(this._oldProperty, propSchema);
        schema.addProperty(this._propertyName, propSchema);
        if (this._oldRequired) {
            if (this.isNullOrUndefined(schema.required)) {
                schema.required = ([]);
            }
            /* add */ (schema.required.push(this._propertyName) > 0);
        }
    };
    return DeletePropertyCommand;
}(AbstractCommand));
export { DeletePropertyCommand };
DeletePropertyCommand["__class"] = "io.apicurio.datamodels.cmd.commands.DeletePropertyCommand";
DeletePropertyCommand["__interfaces"] = ["io.apicurio.datamodels.cmd.ICommand"];
//# sourceMappingURL=DeletePropertyCommand.js.map