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 { Extension } from '../../core/models/Extension';
/**
 * A command used to delete a single extension.
 * @author eric.wittmann@gmail.com
 * @extends AbstractCommand
 * @class
 */
var DeleteExtensionCommand = /** @class */ (function (_super) {
    __extends(DeleteExtensionCommand, _super);
    function DeleteExtensionCommand(extension) {
        var _this = this;
        if (((extension != null && extension instanceof Extension) || extension === null)) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._name === undefined)
                _this._name = null;
            if (_this._hasOldValue === undefined)
                _this._hasOldValue = false;
            if (_this._oldValue === undefined)
                _this._oldValue = null;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._name === undefined)
                _this._name = null;
            if (_this._hasOldValue === undefined)
                _this._hasOldValue = false;
            if (_this._oldValue === undefined)
                _this._oldValue = null;
            (function () {
                _this._parentPath = Library.createNodePath(extension.parent());
                _this._name = extension.name;
            })();
        }
        else if (extension === undefined) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._name === undefined)
                _this._name = null;
            if (_this._hasOldValue === undefined)
                _this._hasOldValue = false;
            if (_this._oldValue === undefined)
                _this._oldValue = null;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._name === undefined)
                _this._name = null;
            if (_this._hasOldValue === undefined)
                _this._hasOldValue = false;
            if (_this._oldValue === undefined)
                _this._oldValue = 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
     */
    DeleteExtensionCommand.prototype.execute = function (document) {
        LoggerCompat.info("[DeleteExtensionCommand] Executing.");
        this._oldValue = null;
        this._hasOldValue = false;
        var parent = this._parentPath.resolve(document);
        if (this.isNullOrUndefined(parent)) {
            return;
        }
        var extension = parent.getExtension(this._name);
        if (ModelUtils.isDefined(extension)) {
            this._hasOldValue = true;
            this._oldValue = extension.value;
            parent.removeExtension(this._name);
        }
        else {
            this._hasOldValue = false;
        }
    };
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    DeleteExtensionCommand.prototype.undo = function (document) {
        LoggerCompat.info("[DeleteExtensionCommand] Reverting.");
        var parent = this._parentPath.resolve(document);
        if (this.isNullOrUndefined(parent)) {
            return;
        }
        var extension = parent.getExtension(this._name);
        if (this._hasOldValue && ModelUtils.isDefined(extension)) {
            extension.value = this._oldValue;
        }
        if (this._hasOldValue && this.isNullOrUndefined(extension)) {
            var ext = parent.createExtension();
            ext.name = this._name;
            ext.value = this._oldValue;
            parent.addExtension(this._name, ext);
        }
    };
    return DeleteExtensionCommand;
}(AbstractCommand));
export { DeleteExtensionCommand };
DeleteExtensionCommand["__class"] = "io.apicurio.datamodels.cmd.commands.DeleteExtensionCommand";
DeleteExtensionCommand["__interfaces"] = ["io.apicurio.datamodels.cmd.ICommand"];
//# sourceMappingURL=DeleteExtensionCommand.js.map