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 { SimplifiedType } from '../models/SimplifiedType';
import { SimplifiedTypeUtil } from '../util/SimplifiedTypeUtil';
import { LoggerCompat } from '../../compat/LoggerCompat';
import { Oas30MediaType } from '../../openapi/v3/models/Oas30MediaType';
/**
 * @author eric.wittmann@gmail.com
 * @extends AbstractCommand
 * @class
 */
var ChangeMediaTypeTypeCommand = /** @class */ (function (_super) {
    __extends(ChangeMediaTypeTypeCommand, _super);
    function ChangeMediaTypeTypeCommand(mediaType, newType) {
        var _this = this;
        if (((mediaType != null && mediaType instanceof Oas30MediaType) || mediaType === null) && ((newType != null && newType instanceof SimplifiedType) || newType === null)) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._mediaTypePath === undefined)
                _this._mediaTypePath = null;
            if (_this._mediaTypeName === undefined)
                _this._mediaTypeName = null;
            if (_this._newType === undefined)
                _this._newType = null;
            if (_this._changed === undefined)
                _this._changed = false;
            if (_this._oldMediaTypeSchema === undefined)
                _this._oldMediaTypeSchema = null;
            if (_this._mediaTypePath === undefined)
                _this._mediaTypePath = null;
            if (_this._mediaTypeName === undefined)
                _this._mediaTypeName = null;
            if (_this._newType === undefined)
                _this._newType = null;
            if (_this._changed === undefined)
                _this._changed = false;
            if (_this._oldMediaTypeSchema === undefined)
                _this._oldMediaTypeSchema = null;
            (function () {
                _this._mediaTypeName = mediaType.getName();
                _this._mediaTypePath = Library.createNodePath(mediaType);
                _this._newType = newType;
            })();
        }
        else if (mediaType === undefined && newType === undefined) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._mediaTypePath === undefined)
                _this._mediaTypePath = null;
            if (_this._mediaTypeName === undefined)
                _this._mediaTypeName = null;
            if (_this._newType === undefined)
                _this._newType = null;
            if (_this._changed === undefined)
                _this._changed = false;
            if (_this._oldMediaTypeSchema === undefined)
                _this._oldMediaTypeSchema = null;
            if (_this._mediaTypePath === undefined)
                _this._mediaTypePath = null;
            if (_this._mediaTypeName === undefined)
                _this._mediaTypeName = null;
            if (_this._newType === undefined)
                _this._newType = null;
            if (_this._changed === undefined)
                _this._changed = false;
            if (_this._oldMediaTypeSchema === undefined)
                _this._oldMediaTypeSchema = 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
     */
    ChangeMediaTypeTypeCommand.prototype.execute = function (document) {
        LoggerCompat.info("[ChangeMediaTypeTypeCommand] Executing.");
        this._changed = false;
        var mediaType = this._mediaTypePath.resolve(document);
        if (this.isNullOrUndefined(mediaType)) {
            return;
        }
        if (this.isNullOrUndefined(mediaType.schema)) {
            this._oldMediaTypeSchema = null;
            mediaType.schema = mediaType.createSchema();
        }
        else {
            this._oldMediaTypeSchema = Library.writeNode(mediaType.schema);
        }
        SimplifiedTypeUtil.setSimplifiedType(mediaType.schema, this._newType);
        this._changed = true;
    };
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    ChangeMediaTypeTypeCommand.prototype.undo = function (document) {
        LoggerCompat.info("[ChangeMediaTypeTypeCommand] Reverting.");
        if (!this._changed) {
            return;
        }
        var mediaType = this._mediaTypePath.resolve(document);
        if (this.isNullOrUndefined(mediaType)) {
            return;
        }
        if (this.isNullOrUndefined(this._oldMediaTypeSchema)) {
            mediaType.schema = null;
        }
        else {
            mediaType.schema = mediaType.createSchema();
            Library.readNode(this._oldMediaTypeSchema, mediaType.schema);
        }
    };
    return ChangeMediaTypeTypeCommand;
}(AbstractCommand));
export { ChangeMediaTypeTypeCommand };
ChangeMediaTypeTypeCommand["__class"] = "io.apicurio.datamodels.cmd.commands.ChangeMediaTypeTypeCommand";
ChangeMediaTypeTypeCommand["__interfaces"] = ["io.apicurio.datamodels.cmd.ICommand"];
//# sourceMappingURL=ChangeMediaTypeTypeCommand.js.map