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 { ExtensibleNode } from '../../core/models/ExtensibleNode';
/**
 * Command that sets an extension.
 * @author eric.wittmann@gmail.com
 * @extends AbstractCommand
 * @class
 */
var SetExtensionCommand = /** @class */ (function (_super) {
    __extends(SetExtensionCommand, _super);
    function SetExtensionCommand(parent, name, value) {
        var _this = this;
        if (((parent != null && parent instanceof ExtensibleNode) || parent === null) && ((typeof name === 'string') || name === null) && ((value != null) || value === 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._value === undefined)
                _this._value = 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._value === undefined)
                _this._value = null;
            if (_this._hasOldValue === undefined)
                _this._hasOldValue = false;
            if (_this._oldValue === undefined)
                _this._oldValue = null;
            (function () {
                _this._parentPath = Library.createNodePath(parent);
                _this._name = name;
                _this._value = value;
            })();
        }
        else if (parent === undefined && name === undefined && value === 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._value === undefined)
                _this._value = 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._value === undefined)
                _this._value = 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
     */
    SetExtensionCommand.prototype.execute = function (document) {
        LoggerCompat.info("[SetExtensionCommand] 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;
            extension.value = this._value;
        }
        else {
            this._hasOldValue = false;
            this._oldValue = null;
            extension = parent.createExtension();
            extension.name = this._name;
            extension.value = this._value;
            parent.addExtension(this._name, extension);
        }
    };
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    SetExtensionCommand.prototype.undo = function (document) {
        LoggerCompat.info("[SetExtensionCommand] 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 && ModelUtils.isDefined(extension)) {
            parent.removeExtension(this._name);
        }
    };
    return SetExtensionCommand;
}(AbstractCommand));
export { SetExtensionCommand };
SetExtensionCommand["__class"] = "io.apicurio.datamodels.cmd.commands.SetExtensionCommand";
SetExtensionCommand["__interfaces"] = ["io.apicurio.datamodels.cmd.ICommand"];
//# sourceMappingURL=SetExtensionCommand.js.map